根据字段条件增加一列

有一个表student的结构如下:

 sid          sname            ssex
 ===========================
   1            张三               1
   2            李四               1
   3            刘红               0
   4            杨静               0
   5            李星               1

要求写一个sql语句显示出以下效果:

 sid          sname            ssex        性别
====================================
   1            张三               1             男
   2            李四               1             男
   3            刘红               0             女
   4            杨静               0             女
   5            李星               1             男

解决方案:

select   case   when   列>'条件值'   then   '值1'   else   '值2'   end   from   表

即:

select *,case when ssex=1     then ‘男’ else ‘女’ end as ‘性别’ from student

猜你喜欢

转载自blog.51cto.com/1197822/2154217