说明:本文测试时使用的MySQL版本是5.7.22
对于5.7.22版本的MySQL,用order by排序的字段不一定要在select语句中,即使是select distinct与order by同时使用。
现有一张记录学生成绩的grade表:
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 1 | 张无忌 | 85 |
| 2 | 李隆基 | 59 |
| 3 | 王五 | 60 |
| 4 | 曹操 | 79 |
| 5 | 小明 | 90 |
| 6 | 如花 | 60 |
| 7 | 尉迟恭 | 100 |
| 8 | 欧阳风 | 90 |
| 9 | 刘备 | 90 |
| 10 | 董永 | 99 |
| 11 | 冯钰 | 83 |
| 12 | 孙殿英 | 82 |
| 2 | 李隆基 | 59 |
+------+--------+-------+
从grade表中查询出学生的id,姓名信息,并且按照score降序排序:
从图中可以看出查询结果是正确的。
从grade表中查询出不同学生的id,姓名信息,并且按照score降序排序:
从上图可以看出即使select distinct与order by同时使用,查询结果也是正确的,没有出现任何报错。
官方文档中的说明:
MySQL 5.7.5 and up implements detection of functional dependence. If the
ONLY_FULL_GROUP_BY
SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list,
HAVING
condition, or
ORDER BY
list refer to nonaggregated columns that are neither named in the
GROUP BY
clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and
ONLY_FULL_GROUP_BY
is not enabled by default.
If
ONLY_FULL_GROUP_BY
is disabled, a MySQL extension to the standard SQL use of
GROUP BY
permits the select list,
HAVING
condition, or
ORDER BY
list to refer to nonaggregated columns even if the columns are not functionally dependent on
GROUP BY
columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an
ORDER BY
clause. Result set sorting occurs after values have been chosen, and
ORDER BY
does not affect which value within each group the server chooses. Disabling
ONLY_FULL_GROUP_BY
is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the
GROUP BY
are the same for each group.
在MySQL5.7.22的配置文件中,SQL mode的默认值为:
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
本文介绍
MySQL
数据库
中
执行
select
查询
语句
,并对查询的结果
使用
order
by 子句进行
排序
。
再来回顾一下SQL
语句
中
的
select
语句
的语法:
Select
语句
的基本语法:
Select
<列的集合> from <表名> where <条件>
order
by <
排序
字段
和方式>
如果要对查询结果按某个
字段
排序
,则要
使用
order
by 子句,如下:
select
* from <表名>
order
by <
字段
名称> <
排序
方式>
下面来看两个例子,第一个查询test表
中
所有数据,并按t_id正序排列;第二个查询与第一个相反,是逆序排列。
mysql
>
select
t_id,
MySQL
讲义第23讲——
select
查询之
ORDER
BY
ORDER
BY
语句
用于对查询结果进行
排序
。默认按照升序对记录进行
排序
,如果希望按照降序对记录进行
排序
,可以
使用
DESC 关键字。语法格式如下:
ORDER
BY
字段
名或表达式 [DESC] [,...]
一、按单个
字段
排序
1、查询所有学生信息,按姓名
排序
mysql
>
SELECT
-> FROM
-> stu
->
ORDER
BY
-> con
第一,在
mysql
中
distinct
的执行顺序高于
order
by。
第二,
distinct
执行时会对查询的记录进行去重,产生一张虚拟的临时表;
第三,
order
by执行时对查询的虚拟临时表进行
排序
,产生新的虚拟临时表。
综合来看,如果
order
by的
字段
不在
select
中
,执行sql
语句
时首先执行
distinct
,之后产生的虚拟临时表
中
没有
order
by的
字段
,所以再执行or
base_customer_info c
INNER JOIN base_customer_project_mapping cp ON cp.customer_global_id = c.global_id
INNER JOIN base_project_info p ON p.global_id = cp.proj
SELECT
DISTINCT
guid FROM table1 WHERE user=?
ORDER
BY id DESC
这个 SQL
语句
在
MySQL
5.6 以及之前的版本都可以正确执行。但是跑到
MySQL
5.7 上就报什么
ORDER
BY 的
字段
不在
SELECT
的列表
中
的 SQL 异常。
解决的办法是修改
MySQL
5.7 的 s...