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查询可能返回不同顺序的行

posted @   锐洋智能  阅读(323)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
历史上的今天:
2009-03-10 【启发】不用循环计算1到100的和
2009-03-10 JS函数(全)
点击右上角即可分享
微信分享提示