Is there a way to get MySQL 5.7/MariaDB to return 0 or 1 for rows that exist in a where IN() query?
like maybe SUM/IF/complex subquery SQL trickery? It could be the id or just a simple 1/0 to know which exists or not.
I thought if I could just add IFNULL(id,0) to a empty row it would return 4 results with 0, but it only shows 3:
Imagine a simple id table:
SELECT * FROM tbl; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec)
EXAMPLE QUERY:
SELECT IFNULL(id,0) FROM tbl WHERE id in (1,2,3,4);
EXPECTED RESULTS
+----+ | id | +----+ | 1 | | 2 | | 3 | | 0 | ------ 4 results
ACTUAL
+----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.01 sec) --------
Basically what I'm trying to do is to return both the existing id and empty ids and not use [temporary] tables, it could be a complex subquery using IN() or FIND_IN_SET or combination of where IN() and EXISTS or NOT EXISTS, etc, etceven a stored procedure, or possibly JSON trickery, etc.