Loading

mysql>>在select里面使用逻辑条件,如 IF-else


+--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 10001 | 60117 | 1986-06-26 | 1987-06-26 |
| 10001 | 62102 | 1987-06-26 | 1988-06-25 |
| 10001 | 66074 | 1988-06-25 | 1989-06-25 |
| 10001 | 66596 | 1989-06-25 | 1990-06-25 |
| 10001 | 66961 | 1990-06-25 | 1991-06-25 |
| 10001 | 71046 | 1991-06-25 | 1992-06-24 |
| 10001 | 74333 | 1992-06-24 | 1993-06-24 |
| 10001 | 75286 | 1993-06-24 | 1994-06-24 |
| 10001 | 75994 | 1994-06-24 | 1995-06-24 |
| 10001 | 76884 | 1995-06-24 | 1996-06-23 |
+--------+--------+------------+------------+

  • 查询工资低于 50000的标记为under
  • 查询工资高于 60000的标记为over
  • 其余记为ok

select emp_no,

       salary,
       case
           when salary <= 50000 then 'underpaid'
           when salary >= 60000 then 'overpaid'
           else
               'ok' end as status
from salaries

 

posted @ 2022-03-11 11:20  夷某蓁  阅读(625)  评论(0)    收藏  举报