1. 程式人生 > 實用技巧 >sql case when的使用

sql case when的使用

目錄

最近在sql使用中,發現 case when 的功能相當強大。

可以根據現有欄位定義新的欄位,可以對新欄位進行排序等等。

下面簡單舉例說明。

用來測試的資料表內容如下:

mysql> select * from test_student;
+----+------+-----+--------+-------+
| id | name | age | sex    | score |
+----+------+-----+--------+-------+
| 19 | John |  18 | male   |    90 |
| 20 | Lily |  17 | female |    89 |
| 21 | Jim  |  17 | male   |    92 |
| 22 | Alex |  16 | male   |    70 |
| 23 | Bell |  19 | ''     |    68 |
+----+------+-----+--------+-------+
5 rows in set (0.00 sec)

例子1

按照性別查詢,定義新欄位性別

mysql> select id, name, case sex when 'male' then '男' when 'female' then '女' else 'unknown' end as '性別' from test_student;
+----+------+---------+
| id | name | 性別    |
+----+------+---------+
| 19 | John | 男      |
| 20 | Lily | 女      |
| 21 | Jim  | 男      |
| 22 | Alex | 男      |
| 23 | Bell | unknown |
+----+------+---------+
5 rows in set (0.00 sec)

或者使用如下方式:

mysql> select id, name, case when sex = 'male' then '男' when sex = 'female' then '女' else 'unknown' end as '性別' from test_student;
+----+------+---------+
| id | name | 性別    |
+----+------+---------+
| 19 | John | 男      |
| 20 | Lily | 女      |
| 21 | Jim  | 男      |
| 22 | Alex | 男      |
| 23 | Bell | unknown |
+----+------+---------+
5 rows in set (0.00 sec)

例子2

下面的例子中,使用兩個欄位進行組合case when。

sexmale,並且 score > 90,則加上標籤smart boy

sexfemale,並且 score > 90,則加上標籤smart girl

其他情況,則為unknown


mysql> select id, name, age, sex, score, case when sex = 'male' and score > 90 then 'smart boy' when sex = 'female' and score > 90 then 'smart girl' else 'unknown' end as 'label' from test_student;
+----+------+-----+--------+-------+-----------+
| id | name | age | sex    | score | label     |
+----+------+-----+--------+-------+-----------+
| 19 | John |  18 | male   |    90 | unknown   |
| 20 | Lily |  17 | female |    89 | unknown   |
| 21 | Jim  |  17 | male   |    92 | smart boy |
| 22 | Alex |  16 | male   |    70 | unknown   |
| 23 | Bell |  19 | ''     |    68 | unknown   |
+----+------+-----+--------+-------+-----------+
5 rows in set (0.00 sec)

例子3

定義新欄位,並按照新欄位排序。

按照score區間進行劃分:

  • score > 90 等級1
  • 80 < score <= 90 等級2
  • 70 < score <= 80 等級3
  • score <= 70 等級4

並按照等級進行排序。

mysql> select id,name,age, sex,score, case when score > 90 then 1 when score > 80 and score <= 90 then 2 when score > 70 and score <= 80 then 3 else 4 end as honor_level from test_student order by honor_level;
+----+------+-----+--------+-------+-------------+
| id | name | age | sex    | score | honor_level |
+----+------+-----+--------+-------+-------------+
| 21 | Jim  |  17 | male   |    92 |           1 |
| 19 | John |  18 | male   |    90 |           2 |
| 20 | Lily |  17 | female |    89 |           2 |
| 22 | Alex |  16 | male   |    70 |           4 |
| 23 | Bell |  19 | ''     |    68 |           4 |
+----+------+-----+--------+-------+-------------+
5 rows in set (0.00 sec)

再例如,

根據sex欄位,定義新欄位sex_int,並按照sex_int進行排序:

mysql> select *, case when sex = 'male' then 1 when sex = 'female' then 2 else 3 end as sex_int from test_student order by sex_int;
+----+------+-----+--------+-------+---------+
| id | name | age | sex    | score | sex_int |
+----+------+-----+--------+-------+---------+
| 19 | John |  18 | male   |    90 |       1 |
| 21 | Jim  |  17 | male   |    92 |       1 |
| 22 | Alex |  16 | male   |    70 |       1 |
| 20 | Lily |  17 | female |    89 |       2 |
| 23 | Bell |  19 | ''     |    68 |       3 |
+----+------+-----+--------+-------+---------+
5 rows in set (0.00 sec)