case…when…使用心得
case when类似于python中的if else语句,但经过最近一段时间使用,我觉得其实更像是完成了一种关系的映射。接下来先说语法吧,自己也说的不太清楚,先总结看看吧。以下表为例。
语法一
case when 列名condition then 输出结果 when 列名condition then 输出结果 … else 输出结果 end;
利用此语法将id化成对应的英文
select case when id=1 then ‘one’ when id=2 then ‘two’ when id=3 then ‘three’ else ‘four’ end, t.name, t.age, t.address, t.salary from company t;
语法二
case 列名 when 列名中的值 then 输出结果 when 列名中的值 then 输出结果 … else