mysql union后group by 实现先排序

公司让写一个报表,有如下sql:

SELECT id,time FROM (
	SELECT aid AS id,atime AS time FROM a
	UNION ALL
	SELECT bid AS id,btime AS time FROM b
) AS a

然后查处如下记录:

[
	{
		"id":2,
		"time":10
	},
	{
		"id":1,
		"time":20
	},
	{
		"id":1,
		"time":30
	},
	{
		"id":1,
		"time":40
	},
]

然后id重复的记录希望取time值最大的
这是改变后的sql:

SELECT id,time FROM (
	SELECT aid AS id,atime AS time FROM a
	UNION ALL
	SELECT bid AS id,btime AS time FROM b
) AS a GROUP BY a.id ORDER BY time DESC

可是查到的并不是我想要的数据

[
	{
		"id":2,
		"time":10
	},
	{
		"id":1,
		"time":20
	}
]

然后我们发现后面的order by 其实并没有起作用
接下来我们把sql修改如下:

SELECT id,time FROM (
	SELECT id,time FROM (
		SELECT aid as id,atime as time FROM a
		UNION ALL
		SELECT bid as id,btime as time FROM b
	) AS b ORDER BY time DESC
) AS a GROUP BY a.id 

发现结果还是一样的,order by 还是没有起作用
敲黑板
最后我了解到,如果order by 不带limit,会被优化器干掉,导致语句就是:

SELECT id,time FROM (
	SELECT id,time FROM (
		SELECT aid as id,atime as time FROM a
		UNION ALL
		SELECT bid as id,btime as time FROM b
	) AS b 
) AS a GROUP BY a.id 

解决方案:

SELECT id,time FROM (
	SELECT id,time FROM (
		SELECT aid as id,atime as time FROM a
		UNION ALL
		SELECT bid as id,btime as time FROM b
	) AS b ORDER BY time DESC LIMIT 999
) AS a GROUP BY a.id 

至此,完美解决

至于为什么出现这种情况,我只找到这一段话

If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.

One factor that affects the execution plan is LIMIT, so an ORDER BY query with and without LIMIT may return rows in different orders. Consider this query, which is sorted by the category column but nondeterministic with respect to the id and rating columns:

自行翻译

有limit和无limit ,order by查询可能返回不同顺序的行

参考博客
https://blog.csdn.net/emaste_r/article/details/73550783
https://www.v2ex.com/t/395218

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值