我有这个问题:
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正如所料。
答案 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