I have a table which looks has the following values:
product_id | custom_id | custom_value |
---|---|---|
1 | 10 | A |
1 | 9 | V |
2 | 10 | B |
3 | 3 | Q |
I am looking for a mysql query to get all values from product_id once and select the row which has custom_id = "10" in case it is available. Nevertheless in case custom_id = 10 is not available for a product_id I would still like to return the product_id but also only once.
So the result I am looking for is
product_id | custom_id | custom_value |
---|---|---|
1 | 10 | A |
2 | 10 | B |
3 | NULL | NULL |
Could please someone direct me in the right direction.
select product_id, custom_id, custom_value from table where custom_id = 10
does of course only return the values for product_id "1" and "2"
2
Answers
You can select the first set of rows, then union by a distinct of all the other product id’s
You can first generate a
ROW_NUMBER
to get the first element for each "product_id", then transform to NULL values for which "product_id" does not match your value 10, using theIF
function.Check the demo here.