时间:2021-07-01 10:21:17 帮助过:3人阅读
在不同版本的MySQL来进行测试:发现在Percona 5.5,Percona 5.1,MySQL 5.6关闭sql_mode= ONLY_FULL_GROUP_BY,MySQL5.1等版本下,返回值确如程序员期待的顺序,按照order by no desc的顺序,相同name返回no值最大的数据;
+----+----+-------+ | id | no | name | +----+----+-------+ | 4 | 4 | Herry | | 2 | 2 | John | | 5 | 5 | Mike | | 3 | 3 | wyett | +----+----+-------+
在mysql5.7,关闭sql_mode= ONLY_FULL_GROUP_BY和mariadb 10.*版本中,相同的name值,返回则是取了最早写入的数据行,忽略了order by no desc,按照数据的逻辑存储顺序来返回;
+----+----+-------+ | id | no | name | +----+----+-------+ | 4 | 4 | Herry | | 2 | 2 | John | | 1 | 1 | Mike | | 3 | 3 | wyett | +----+----+-------+
其实在这里,SQL等价于select id,no,name from testorder group by name。
这里我们看出不同版本的返回值是不同的,先搁置数据量的变化引起执行结果不同的讨论,因为数据量大小很难测试。
对上面的测试结果,在官方文档上,有如下的参考
If ONLY_FULL_GROUP_BY is disabled...In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate, 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.
ONLY_FULL_GROUP_BY这个SQL_MODE出在mysql5.6(mariadb 10.0)时被引入,但本文讨论的内容和它无关,具体可以自己查看文档,这里不做讨论。在5.6,5.5的官方文档有相同的内容,Mariadb也有类似的解释
If you select a non-grouped column or a value computed from a non-grouped column, it is undefined which row the returned value is taken from. This is not permitted if the ONLY_FULL_GROUP_BY SQL_MODE is used.
并且,对from后的subquery子表中的order by也给出了解释
A query such as SELECT field1, field2 FROM ( SELECT field1, field2 FROM table1 ORDER BY field2 ) alias returns a result set that is not necessarily ordered by field2. This is not a bug. A "table" (and subquery in the FROM clause too) is - according to the SQL standard - an unordered set of rows. Rows in a table (or in a subquery in the FROM clause) do not come in any specific order.
好了,有了这些解释,问题很明朗:
在from 后的subquery中的order by会被忽略
group by cloumn返回的行是无序的
因此,业务获得的正确的返回值也是误打误撞。
那么这个问题该怎么解决?
在网上有一些SQL,很明显不满足需求,在这里做一下展示,希望同学们避免被误导:
错误SQL集合
select id,sbustring(GROUP_CONCAT(distinct no order by no desc separator ‘‘),‘‘,1),name from testorder group by name;
--通过添加索引来影响返回的结果集顺序 alter table testorder add index idx_no_name(no desc, name); --结果证明即使如此,desc也不会被正确执行;
--我司程序员的写法 select * from (select id,no,name from testorder order by no desc)a group by a.name
select id,max(no),name from testorder group by name
我们可以这样写,虽然效率不高
select a.id,a.no,a.name from testorder a inner join (select max(no) no,name from testorder group by name) b on a.no=b.no and a.name=b.name group by name,no
或者这样
select a.id,a.no,a.name from testorder a group by a.name,a.no having a.no=(select max(no) from testorder where name=a.name)
本文出自 “wyett的技术角落” 博客,请务必保留此出处http://wyett.blog.51cto.com/9244961/1909546
mysql组内排序取最大值
标签:optimize sql