Z
Z
ZmeY2019-06-13 16:37:54
SQL
ZmeY, 2019-06-13 16:37:54

SQL - how to display non-existent rows as NULL?

Good afternoon!
sql query:
SELECT * FROM table WHERE id IN (1, 2, 3, 4, 5)
displays existing rows with id 1, 3, 5
id value
1 data1
3 data3
5 data5
rows with id 2, 4 - do not exist and respectively, is it
possible to make non-existent rows output as NULL
id value
1 data1
2 NULL
3 data3
4 NULL
5 data5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman, 2019-06-13
@ZmeY

SELECT
 _ids.`_value`, t.`value`
FROM
(SELECT 1 as `_value` UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) as _ids
LEFT JOIN `table` t ON t.`id` = _ids.`_value`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question