翔云

Just try, don't shy. 最新文章请点击
随笔 - 294, 文章 - 0, 评论 - 27, 阅读 - 49万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sql case when的使用

Posted on   翔云123456  阅读(465)  评论(0编辑  收藏  举报

最近在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)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-11-15 golang 故障模拟工具failpoint的使用
点击右上角即可分享
微信分享提示