一、case语句
当当前字段为空,查询结果返回“none”,并且统计出现频率
select case when 字段 is null then 'none' else 字段 end as 字段, count(1) as counts from 表 group by 字段;
当当前字段为空字符串,查询结果返回“none”,并且统计出现频率
select case when 字段= '' then 'none' else 字段 end as 字段, count(1) as counts from 表 group by 字段;
当当前字段为空,查询结果返回“none”
select case when 字段 is null then 'none' else 字段 end as 字段 from 表;
当当前字段为空字符,查询结果返回“none”
select case when 字段= '' then 'none' else 字段 end as 字段 from 表;
二、isnull,ifnull,nullif的用法
1、IFNULL(expr1,expr2)的用法:
假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1;
否则其返回值为 expr2。IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。
IFNULL( c.LEVEL, '小白键盘手' ) AS LEVEL
2、NULLIF(expr1,expr2) 的用法:
如果expr1 = expr2 成立,那么返回值为NULL,否则返回值为 expr1。
3、isnull(expr) 的用法:
如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。
本文详细介绍了SQL中的CASE语句及其在处理空值时的应用,包括当字段为空或空字符串时如何返回'none'并进行频率统计。此外,还讲解了IFNULL、NULLIF和ISNULL函数的用法,帮助理解如何在SQL中有效地处理和检查空值。这些知识点对于数据库查询和数据清洗至关重要。

被折叠的 条评论
为什么被折叠?



