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

  • 相关阅读:
    ansible(十)roles
    playbook变量(九)for循环
    playbook变量(八)循环迭代
    playbook变量(七)template 基本使用 when
    playbook变量(六)template 基本使用
    playbook变量(五)
    Sqoop2常用命令介绍
    Sqoop2入门之导入关系型数据库数据到HDFS上
    Sqoop2环境搭建
    Spark On Yarn中spark.yarn.jar属性的使用
  • 原文地址:https://www.cnblogs.com/interdrp/p/15987869.html
Copyright © 2020-2023  润新知