惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

Hsu Yeung 的博客

三圣花市 | Hsu Yeung 的博客 网站支持 Live Photo 图片展示 | Hsu Yeung 的博客 梦 | Hsu Yeung 的博客 周末午餐 | Hsu Yeung 的博客 张家界 | Hsu Yeung 的博客 话剧《寻她芳踪·张爱玲》 | Hsu Yeung 的博客 博客自动生成文章目录 | Hsu Yeung 的博客 灭螂行动! | Hsu Yeung 的博客 张靓颖成都演唱会 | Hsu Yeung 的博客 最近对博客做的一些微调总结 | Hsu Yeung 的博客 Windows 上安装 Lua | Hsu Yeung 的博客 网站支持展示 B 站 iframe 视频 SpringBoot 中使用 RedisTemplate 存取整数值的坑 | Hsu Yeung 的博客 SQL JOIN 中 ON 与 WHERE 的区别 值得记录的一些工作近况 | Hsu Yeung 的博客 周传雄成都演唱会 | Hsu Yeung 的博客 并行操作导致获取数据库连接超时 | Hsu Yeung 的博客 风信子 | Hsu Yeung 的博客 Linux 安装并配置 Nginx | Hsu Yeung 的博客 使用 logrotate 切割 nginx 日志 郁金香土培结果 | Hsu Yeung 的博客 MySQL LEFT JOIN 右表有多条数据但只取最新的一条 | Hsu Yeung 的博客 获取数据库锁等待超时问题 | Hsu Yeung 的博客 从零开始挑选相机 | Hsu Yeung 的博客 郁金香 | Hsu Yeung 的博客 我的第一束花 | Hsu Yeung 的博客 快乐的一周 | Hsu Yeung 的博客 重庆,来了 | Hsu Yeung 的博客 散步 | Hsu Yeung 的博客 Git 工作区、暂存区、版本库之间的关系 | Hsu Yeung 的博客 Collectors.toMap() 操作 value 为 null 的情况 AOP 记录请求参数时序列化异常问题 | Hsu Yeung 的博客 第一次参加演唱会 | Hsu Yeung 的博客 MySQL FIND_IN_SET 函数的使用 | Hsu Yeung 的博客 自动拆箱机制导致编译失败 | Hsu Yeung 的博客 最近 | Hsu Yeung 的博客 夜爬龙泉山 | Hsu Yeung 的博客 在 Windows 上安装多个版本 JDK 并切换使用的版本 安装 Windows Terminal 和 gsudo 记录 意外的奖励 | Hsu Yeung 的博客 记第一次修热水器 | Hsu Yeung 的博客 在 Windows 电脑上调试 iPhone Safari 浏览器的网页 给网站加上 RSS | Hsu Yeung 的博客 打雷了 | Hsu Yeung 的博客 豉汁蒸排骨 | Hsu Yeung 的博客 我的 2022 年 | Hsu Yeung 的博客 如何使用 Git 给代码打 tag | Hsu Yeung 的博客 记一次有趣的上班经历 | Hsu Yeung 的博客 我记得 | Hsu Yeung 的博客 Spring 新增或修改请求 header 参数 | Hsu Yeung 的博客 Spring 事务传播机制 | Hsu Yeung 的博客 在家自制“周黑鸭” | Hsu Yeung 的博客 晚餐 | Hsu Yeung 的博客 如何解决逻辑删除与数据库唯一约束冲突 | Hsu Yeung 的博客 [转]漫画赏析:Linux 内核到底长啥样 | Hsu Yeung 的博客 数据库逻辑设计之三大范式通俗理解 | Hsu Yeung 的博客 计算机中的大小端是指什么 | Hsu Yeung 的博客 MySQL 学习笔记 | Hsu Yeung 的博客 [转]《玉子市场》的人物魅力 | Hsu Yeung 的博客 Linux 安装 Redis | Hsu Yeung 的博客 Linux 安装 MySQL | Hsu Yeung 的博客 Linux 安装 JDK | Hsu Yeung 的博客 [转]Thymeleaf 表达式工具类 | Hsu Yeung 的博客 Shell 基础入门介绍 | Hsu Yeung 的博客 Linux 中的软链接和硬链接 | Hsu Yeung 的博客 数据结构与算法学习笔记 | Hsu Yeung 的博客 Visual Studio Code 如何编写运行 C 程序?
MySQL 将某些字段的值排在最前或最后 | Hsu Yeung 的博客
2020-07-01 · via Hsu Yeung 的博客

有时候使用 MySQL 查询记录的时候需要将一些指定字段值的数据排在最前面或者最后面。

本文介绍两种语法来实现这个功能。

1. 将指定字段值的数据排在最后面

要求:将 subject 值为 'Physics' 和 'Chemistry' 的数据排在最后

方法一:使用 column in ('value1', 'value2', ..., 'valuen')

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY subject IN ('Physics','Chemistry'), subject, winner;

方法二:使用 case 子句

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY
    (case subject
         when 'Chemistry' then 1
         when 'Physics'   then 1
         else 0
        end), subject, winner;

-- 等价于:

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY
    (case subject
         when 'Chemistry' then 0
         when 'Physics'   then 0
         else 1
        end) DESC, subject, winner;
将指定字段值的数据排在最后面-方法 1
将指定字段值的数据排在最后面-方法 1
将指定字段值的数据排在最后面-方法 2
将指定字段值的数据排在最后面-方法 2

2. 将指定字段值的数据排在最前面

排在最前面其实就是和排在最后面取相反的条件即可。

也可以在排在最前面的写法后面加上 DESC 进行倒序排列完成。

要求:将 subject 值为 'Physics' 和 'Chemistry' 的数据排在最前面

方法一:使用 column in ('value1', 'vaule2', ..., 'valuen')

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY subject NOT IN ('Physics','Chemistry'), subject, winner;

方法二:使用 case 子句

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY
    (case subject
         when 'Chemistry' then 0
        when 'Physics'   then 0
        else 1
     end), subject, winner;

-- 等价于:

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY
    (case subject
         when 'Chemistry' then 1
        when 'Physics'   then 1
        else 0
     end) DESC, subject, winner;
将指定字段值的数据排在最前面-方法 1
将指定字段值的数据排在最前面-方法 1
将指定字段值的数据排在最前面-方法 2
将指定字段值的数据排在最前面-方法 2

3. 两种方法的区别

方法一主要是利用了 IN 关键字,判断出如果字段的值在后面的值列表中那么整个表达式的值就为 1, 不在的话整个表达式的值就为 0。 由于默认升序,0 < 1,所以不在值列表里的数据当然就排在前面了,在值列表里的就排在最后面了。

注意:实例中只要求 subject 值为 'Physics' 和 'Chemistry' 的数据排在最前面/最后面,但是值为 'Physics' 和 'Chemistry' 的先后顺序并不是由第一个排序字段来决定了,他们的顺序又是由后面的两个字段来共同决定了。
如果要想精确控制顺序那么可以使用方法二, case 子句给不同的值赋予不同的数字,这样排序时就有个先后之分了。

比如现在要求 'Physics' 和 'Chemistry' 排在最前面,且 'Physics' 要在 'Chemistry' 的前面,只需要保证 'Physics' < 'Chemistry' < else 就可以实现:

SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY
    (case subject
         when 'Chemistry' then 1
        when 'Physics'   then 0
        else 2
     end), subject, winner;
精确控制顺序
精确控制顺序

4. 本文使用的 SQL 练习网站

免费的 SQL 练习网站,支持中文,有需要的可以去在线练习,本文章也的题目也是来源于该网站。

点此访问网站链接