我有下表
Select * from Peeps
Id Name Age
1 Sam 16
2 John NULL
Select
Name,
Age,
(If Age=NULL,1,0) AS Z
from peeps;
结果:
+------+------+---+
| name | age | Z |
+------+------+---+
| Sam | 16 | 0 |
| John | NULL | 0 |
+------+------+---+
期望的结果
+------+------+---+
| name | age | Z |
+------+------+---+
| Sam | 16 | 0 |
| John | NULL | 1 |
+------+------+---+
答案 0 :(得分:4)
尝试:
Select
Name,
Age,
(If Age IS NULL,1,0) AS Z
from peeps;
http://dev.mysql.com/doc/refman/5.1/en/working-with-null.html
答案 1 :(得分:3)
请改用:
IF Age IS NULL
使用NULL实际上表示空值,而“NULL”将作为字符串,其中包含字母N-U-L-L。完全不一样。
最终查询:
SELECT Name,
Age,
(IF Age IS NULL, 1, 0) AS Z
FROM peeps