Mysql case when then 问题

Smember 2017-07-04 05:08:44

select
case xxx
when 'ABC' then 'abc'
when 'DEF' then 'def'
else 'xxxx' end as xxxx
from table_name


想把上面sql中的字段加一个当为 null 的时候给一个默认值.

这个好像不能用:

select case when xxx is null then '000' from table_name

求各位大大给个办法.
...全文
263 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2017-07-05
  • 打赏
  • 举报
回复
可以嵌套的 select    case xxx      when 'ABC' then 'abc'     when 'DEF' then 'def'   else case when xxx is null then '000'  else 'xxxx'  end end as xxxx from table_name 
二月十六 2017-07-04
  • 打赏
  • 举报
回复
select 
  case  
    when xxx = 'ABC' then 'abc'
    when xxx = 'DEF' then 'def'
    when xxx is null then '000'
  else 'xxxx' end as xxxx
from table_name

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧