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

  • 相关阅读:
    HTML入门(一)
    WEB攻击手段及防御第2篇-SQL注入
    公司来了个新同事不会用 Lombok,还说我代码有问题!
    最流行的 RESTful API 要怎么设计?
    Spring Boot & Restful API 构建实战!
    分布式事务不理解?一次给你讲清楚!
    带着问题学 Kubernetes 架构!
    Linux 与 Unix 到底有啥区别和联系?
    Java虚拟机最多支持多少个线程?
    常用的 Git 命令,给你准备好了!
  • 原文地址:https://www.cnblogs.com/interdrp/p/15987869.html
Copyright © 2020-2023  润新知