CASE WHEN的用法主要分为两种:
CASE 字段名 WHEN "xxxx" THEN "1" else "0" endCASE WHEN 字段名 = "xxx" THEN "1" else "0" end
相信会点进来观看的读者都是使用第一种方式进行判断的,但这种方式确实不能对Null值进行判断,而且在执行的时候还会报语法错误,此时我们应该使用第二种写法,如下:
CASE WHEN 字段名 is Null THEN "1" else "0" end
真正放到一个数据库查询语句如下:
#单条件判断select id,case when name is Null then '1' else "0" endfrom c_student#多添加判断select id,name,case when sex is null then '1'whensex = '男' then '1'else "0" endfrom c_student