mysql 的 case when then 用法 和null 的判断!

  • A+
所属分类:SQL技巧

mysql 的 case when then 用法 和null 的判断!

 

表:一个表 aa 有两个字段 id 和 sex ,第1条记录的sex 为空串  ('')  第二条记录的sex 为空  (null)

mysql 的 case when then 用法 和null 的判断!

 

1. 用法:

第一种: select (case 字段名  when 字段值1  then 结果  when 字段值2 then 结果2  else (默认值) end )

举例:

mysql 的 case when then 用法 和null 的判断!

select id ,(case sex  when ''  then 'bbbbb'                      when  null then 'aaaaa'                             else sex end  ) as sex FROM aa;

 

 

这个结果是有问题的,理想的结果第二条记录为2 aaaaa ,但是确为空,说明这个判断null 条件有问题,

经过测试:判断null 要用is null

 

mysql 的 case when then 用法 和null 的判断!

 

 

 

第二种: select (case  when 判断条件1  then 结果  when 判断条件2 then 结果2  else (默认值) end )

 

 

select id ,(case   when sex= ''  then 'bbbbb'                      when sex is null then 'aaaaa'                             else sex end  ) as sex FROM aa;

 

快来试试吧。

学习资料见知识星球。

以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。

快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利​​​​!

更多技巧, www.excelbook.cn

欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;

mysql 的 case when then 用法 和null 的判断!

你将获得:

1、价值上万元的专业的PPT报告模板。

2、专业案例分析和解读笔记。

3、实用的Excel、Word、PPT技巧。

4、VIP讨论群,共享资源。

5、优惠的会员商品。

6、一次付费只需99元,即可下载本站文章涉及的文件和软件。

  • 我的微信
  • weinxin
  • 我的知识星球
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: