D
D
des1roer2014-12-16 11:33:59
PostgreSQL
des1roer, 2014-12-16 11:33:59

How to select data depending on id?

how to convert queries like
SELECT "F_Value" FROM "SC_Wonder"."T_Passport" WHERE "ID_Product" = 2
SELECT "F_Value" FROM "SC_Wonder"."T_Passport" WHERE "ID_Product" = 3
into one so that I specifically know what the first number is will be the value with id 2 , the second with 3 and so on?
UPD
SELECT "F_Value" FROM "SC_Wonder"."T_Passport" WHERE "ID_Product" IN (2,3) ORDER BY ID_Product;
and if there is no value with id 2, how to return zero?
I can't guarantee that the ids will be strictly larger. you need to know that an element with a certain id will come in a strictly defined account or null will come

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
red_led, 2014-12-16
@red_led

If I understand you correctly, then:

SELECT /* v.id, */ "F_Value" 
FROM (VALUES (1), (2)) v(id)
LEFT JOIN "SC_Wonder"."T_Passport" p on v.id = p.ID_Product
ORDER BY ID_Product;

First, a temporary table is created, in which each line is the desired ID_Product. Then, with the help of LEFT JOIN, existing records are selected. For non-existent fields, they will remain filled with nulls. If you uncomment, v.id, you will see which ones ID_Producthave no values.

A
AxisPod, 2014-12-16
@AxisPod

SELECT "F_Value" FROM "SC_Wonder"."T_Passport" WHERE "ID_Product" in (2,3) ORDER BY "ID_Product";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question