For the example table acts
below:
id | request_id |
---|---|
1 | 234 |
2 | 531 |
and the query below:
select request_id, id
from acts
where id in (234,531,876)
I need to get the following result:
request_id | id |
---|---|
234 | 1 |
531 | 2 |
876 | null |
As you could see there is not row where request_id 876 exists. For these cases null
should be returned.
How could I achieve this?
2
Answers
Conditional logic can be helpful in this situation.
Assuming you actually meant to join on
request_id
notid
.The above creates a VALUES list that you can then
LEFT JOIN
toacts
. In that case therequest_id
missing inacts
will show up withNULL
values for the corresponding field inacts
.