码迷,mamicode.com
首页 > 数据库 > 详细

MySQL Case When 用法

时间:2020-07-10 11:39:12      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:bsp   auth   font   end   格式   col   sel   简洁   from   

case具有两种格式。简单case函数和case搜索函数。

1、简单case函数:

    case sex
    when ‘1‘ then ‘男‘
    when ‘2‘ then ‘女‘
    else ‘其他‘ end

2、case搜索函数:

    case when sex = ‘1‘ then ‘男‘
    when sex = ‘2‘ then ‘女‘
    else ‘其他‘ end

这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。 

例如:

select grouping_authorizations.group as ‘授权分组‘,count(id) as ‘总用户数‘,sum(case when is_authorize=1 then 1 else 0 end) as ‘授权用户数‘,sum(case when is_authorize=2 then 1 else 0 end) as ‘未授权用户数‘ from grouping_authorizations group by grouping_authorizations.group;

Enjoy it !

MySQL Case When 用法

标签:bsp   auth   font   end   格式   col   sel   简洁   from   

原文地址:https://www.cnblogs.com/daizhongxing/p/13278215.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!