I need to get a list of IDs and a corresponding field from a table where the ID may not exist. For example, the table looks like:
id | status
-------------
1234 | A
4567 | B
1020 | C
I want to get the status from rows with id=4567 and id=7777, like this:
Result:
id | status
-------------
4567 | B
7777 |
Since there is no record with id=7777 it should show an empty status field.
What I have so far: I can get an empty row when there is no record for any of the ID matches by joining the result with DUAL. For example:
SELECT id, status FROM DUAL LEFT OUTER JOIN mytable ON id='7777'
Gives the result of an empty row:
id | status
-------------
|
But adding a valid id to the condition only returns one row:
SELECT id, status FROM mytable WHERE (id='7777' OR id='4567')
id | status
-------------
4567 | B
How can I make the query return a row with the requested ID even if it has no record?
1 Answer