MySQL组内排序取最大值

最近业务反馈一个查询异常的问题,需要DBA对查询结果异常给出解释,并帮助他们解决该问题。问题本质是一个组内排序取最大值的问题,根据业务需求,我构建了测试用例

测试用例

--建表
create table testorder
(id int not null,
no int not null,
name char(10) not null,
primary key(id)
)engine=innodb;
--写入数据
insert into testorder values (1,1,‘Mike‘),(2,2,‘John‘),(3,3,‘wyett‘),(4,4,‘Herry‘),(5,5,‘Mike‘),(6,1,‘John‘),(7,2,‘John‘),(8,1,‘Mike‘),(9,1,‘Mike‘);
--查询1
select * from testorder;
+----+----+-------+
| id | no | name  |
+----+----+-------+
|  1 |  1 | Mike  |
|  2 |  2 | John  |
|  3 |  3 | wyett |
|  4 |  4 | Herry |
|  5 |  5 | Mike  |
|  6 |  1 | John  |
|  7 |  2 | John  |
|  8 |  1 | Mike  |
|  9 |  1 | Mike  |
+----+----+-------+
--查询2
select * from testorder order by no desc;
+----+----+-------+
| id | no | name  |
+----+----+-------+
|  5 |  5 | Mike  |
|  4 |  4 | Herry |
|  3 |  3 | wyett |
|  2 |  2 | John  |
|  7 |  2 | John  |
|  1 |  1 | Mike  |
|  6 |  1 | John  |
|  8 |  1 | Mike  |
|  9 |  1 | Mike  |
+----+----+-------+
--查询3
select * from (select id,no,name from testorder order by no desc)a group by a.name;

查询3这条SQL是我们需要讨论的内容,也是业务线为实现组内排序取最大值所采用的SQL。标准的程序员反馈问题方式:XXX时间点之前查询时正常的,这之后突然就不正常了,你们DBA是不是做什么改动了?我把数据恢复到自己的测试机,返回值也是正常的。暂且不去管姿势是否正确,对这条SQL的分析,我们其实可以看出:(1)程序员期待group by执行结果是按照临时表a的数据顺序来取值;(2)程序员未考虑版本因素,数据量变化的因素;为此,我构建了上面的测试用例。

测试

在不同版本的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)
时间: 2024-12-07 14:58:46

MySQL组内排序取最大值的相关文章

mysql分组排序取最大值所在行的实现方法

如下图, 计划实现 :按照 parent_code 分组, 取组中code最大值所在的整条记录,如红色部分.(类似hive中: row_number() over(partition by)) select c.* from ( select a.*, (@i := case when @key_i=parent_code then @i+1 else 1 end) as sort_num,(@key_i:=parent_code) as tmp from my_test a, (SELECT

mysql数据库TINYINT取值范围详解

分享下mysql中TINYINT的取值范围,很基础的一些内容. 在MySQL的数据类型中,Tinyint的取值范围是:带符号的范围是-128到127.无符号的范围是0到255(见官方<MySQL 5.1参考手册>http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#numeric-types). Tinyint占用1字节的存储空间,即8位(bit).那么Tinyint的取值范围怎么来的呢?先看无符号的情况.无符号的最小值即全部8位(b

输入一组整数,求子数组和的最大值。(数组进行首尾相接之后)

输入一组整数,求子数组和的最大值. 题目:返回一个一维整数数组中最大子数组的和. 要求: 输入一个一维整形数组,数组里有正数也有负数. 一维数组首尾相接,象个一条首尾相接带子一样. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值. ? 10 -9 8 7 -5 3 ? i : 4 3 2 1 0 ? nALL : 3 3 7 15 15 16 ? nStart: 3 -2 7 15 6 16 ? nStart = max(arr[i], arr[i]+

润乾集算报表非常规统计之组内排序

报表开发中,经常会碰到一些需要进行非常规统计的报表,预置分组.可重复分组.组内排序,还包括跨行组计算的报表,甚至有些报表本身无数据来源.以及需要对数据源再计算.这些报表本身具备一定的特殊性,使用常规方法往往难于实现. 而集算报表在完成这类特殊统计报表时则比较简单,这里来看下使用集算报表完成组内排序报表的实现过程. 报表说明 根据销售管理系统数据统计某年客户所在地区的订单总额,地区并按订单总额降序排列,要求每个地区中显示销售额在前五名的销售人员及其订单数量和订单金额,其他人员归入"其他"

Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)

概要 这篇博客是在上一篇博客Phantomjs+Nodejs+Mysql数据抓取(1.抓取数据) http://blog.csdn.net/jokerkon/article/details/50868880 后进行的第二部分,请各位读者在看这篇博客之前先浏览上一篇,因为这里面有部分代码会沿用到上一部分的抓取结果. 好,现在开始正式的抓取图片的讲解 首先,我们先来看看代码: var page =require('webpage').create(); var address='http://pro

编程之美 2.14求数组的子数组之和的最大值

对于一个有N个元素的数组,a[0]~a[n-1],求子数组最大值. 如:数组A[] = [−2, 1, −3, 4, −1, 2, 1, −5, 4],则连续的子序列[4,−1,2,1]有最大的和6. 方法一:暴力 循环遍历,输出所有,判断最大的和 1 #include"iostream" 2 #define MAX 1001 3 using namespace std; 4 5 int main(){ 6 int n, a[MAX], sum , maxsum ; 7 8 cin &

【编程之美】3.7 队列取最大值操作问题 ☆

之前写过栈的,以为队列的也一样,结果一点都不一样.写了好久啊. 因为栈是后进先出,先进去的数字不会影响后面的数字:而队列是先进先出,后进去的会受先进入的数字的影响. 比如: (先)  1 9 3 8 4 (后)  这样的序列 栈存储        1 9               就可以了,因为9弹出后,自然 1 就是最大的 队列则不行,如果按上面存储9弹出后 剩下 3 8 4,8是最大的,没有存储. 我的方法,保存一个max的队列 入队列时: 如果新值比 max的最前面的元素大,那么把max

编程之美之2.14 求数组的子数组之和的最大值

[题目] 一个有N个整数元素的一维数组(A[0],A[1],A[2],...A[n-1]),这个数组中当然有很多子数组,那么子数组之和的最大值是多少? 该子数组是连续的. 我们先来明确一下题意: (1)子数组意味着是连续的. (2)题目只需要求和,并不需要返回子数组的具体位置. (3)数组的元素是整数,所以数组可能包含正整数,负整数或者零. 举几个例子: 数组:[1,-2,3,5,-3,2]返回8 数组:[0,-2,3,5,-1,2]返回9 数组:[-9,-2,-3,-5,-3]返回8 [解法一

编程之美2.14 求数组的子数组之和的最大值

问题描述: 一个有N个整数元素的一维数组(A[0], A[1], A[2],...,A[n-1]),这个数组当然有很多子数组,那么子数组之和的最大值是什么呢? 解法: 1. 暴力解法-------O(N^3) 2. 改进版暴力解法-------O(N^2) *3. 分治算法-------O(NlogN)(暂时未去实现) 4. 数组间关系法-------O(N) 具体思路和代码: 1.暴力解法 思路:Sum[i,...,j]为数组第i个元素到第j个元素的和,遍历所有可能的Sum[i,...,j].