Returning empty or empty string in "IN CLAUSE" mysql php

I have this request:

SELECT * from tbl WHERE id = 1 AND option IN (1,2,3) 

      

table:

+-------+---------+-------+
| id    | option  | votes |
+-------+---------+-------+
|   1   |    1    |  100  |
|   1   |    2    |  150  |
+-------+---------+-------+

      

As option no.3 doesn't exist in the table yet, I want it to return a null / empty string, so I can set 'votes' to 0 in my php code.

It currently gives me option lines 1 and 2 as expected.

+3


source to share


1 answer


SELECT * from tbl t1
RIGHT JOIN 
(SELECT 1 AS opt UNION ALL
 SELECT 2 AS opt UNION ALL
 SELECT 3 AS opt UNION ALL
) t2
ON t1.option = t2.opt
WHERE t1.id = 1 

      



0


source







All Articles