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 !