有一些数据库表中的性别列会用字符,数字等代替男女汉字,但在进行查询时还要把男女汉字查询出口,有的同学会在后端进行验证,不过那样太麻烦了,在sql中有个很好用的语句 case when then else。
下面直接介绍用法。
语法:
select case when 性别字段名=性别男的代表字符或数字 then '男' when 性别字段名=性别女的代表字符或数字 then '女' end as 性别 from 表名;
select name as 姓名,case when sex='F' then '女' when sex='M' then '男' end as 性别 from t
ok!