正常在sql中对一个字段的空值判断一般为:
select * from 表名 where 字段名 is null;
正常在使用case when的句法时,一般为:
select
(case 字段名
when '1' then '11'
when '2' then '22'
else
'33'
end) test,
*
from
表名
但当需要用case when来判断一个值是否为空时,一开始我用的是
select
(case 字段名
when null then '11'
else
'22'
end) test,
*
from
表名
执行后发现提示错误,然后改成了以下格式之后就正常了。
select
(case
when 字段名 is null then '11'
else
'22'
end) test,
*
from
表名
总结:其实以上其实是case when的两种用法
--------------------------第一种
CASE sex
WHEN '0' THEN '女'
WHEN '1' THEN '男'
ELSE '其他' END
--------------------------第二种
CASE
WHEN sex = '1' THEN '男'
WHEN sex = '0' THEN '女'
ELSE '其他' END
第一种适合做简单的比较,第二种更适合做对列的判断。