I'm running a simple SELECT statement on my PostgreSQL DB that looks like the following:
SELECT * FROM my_table WHERE primary_key IN (a, b, c);
If my_table only has entries corresponding to the primary keys a, and c, then the query will only return two rows - one for a and all its data, and one for c and all its data.
I want the query to return an empty row (or a NULL/nil value) whenever there is no entry for the primary key in the table.
So I want the output of the above SELECT query to be [a_data, nil, b_data] instead of [a_data, b_data]. Is there a way to accomplish this?
More generally, if I have SELECT * FROM my_table WHERE primary_key IN (x_1, x_2, ..., x_n), I want there to be n rows returned, where x_data is NULL/nil for every x that doesn't exist as a key in the DB.
UNIONof all key values and thenLEFT JOINit with your table.SELECTquery on the joined table, right?