1、case when 表达式两种形式
--简单case函数case 'sex' when '1' then '男' when '2' then '女' else '其他' end --case搜索函数case when sex = '1' then '男' when sex = '2' then '女' else '其他' end
2、case when 在语句中不同位置的用法
1)select case when用法
select grade, count(case when sex = 1 then '男' else null end) 男生数, count(case when sex = 2 then '女' else null end) 女生数 from students group by grade;
2)group by case when用法
select case when salary <= 500 then '1' when salary > 500 and salary <= 600 then '2' when salary > 600 and salary <= 800 then '3' when salary > 800 and salary <= 1000 then '4' else numm end salary_calss, count(*) from table_A group by case when salary <= 500 then '1' when salary > 500 and salary <= 600 then '2' when salary > 600 and salary <= 800 then '3' when salary > 800 and salary <= 1000 then '4' else numm end;
3)where case when用法
select t2.*, t1.* from t1, t2where (case when t2.compare_type = 'A' and t1.some_type like 'NOTHING%' then 1 when t2.compare_type = 'A' and t1.some_type not like 'NOTHING%' then 2 else 0 end ) = 1