在“IN CLAUSE”mysql php中返回null或空字符串

时间:2017-05-16 14:34:09

标签: php mysql

我有这个问题:

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

表:

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

由于选项no.3在表中尚不存在,我希望它返回null /空字符串,所以我可以在我的php代码中将'votes'设置为0。

目前它只给我一些选项1& 2正如所料。

1 个答案:

答案 0 :(得分:0)

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