【问题标题】:MySQL: Undesired result with max function on a timestampMySQL:时间戳上具有最大功能的不良结果
【发布时间】:2017-01-12 12:14:15
【问题描述】:

我使用 Mantis 错误数据库(使用 MySQL),我想查询在过去 2 周内哪些错误的严重性发生了变化,但只应指出错误的最后一次严重性变化。

问题是,每个 bugID(这是主键)我得到多个条目,这不是我想要的结果,因为我只想对每个 bug 进行最新的更改。这意味着我以某种方式错误地使用了 max 函数和 group by 子句。

在这里你可以看到我的查询:

SELECT `bug_id`,
    max(date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %h:%i:%s')) AS `Severity_changed`,
    `mantis_bug_history_table`.`old_value`,
    `mantis_bug_history_table`.`new_value`
    from `prepared_bug_list` 
    join `mantis_bug_history_table` on `prepared_bug_list`.`bug_id` = `mantis_bug_history_table`.`bug_id` 
    where (`mantis_bug_history_table`.`field_name` like 'severity') 
    group by `bug_id`,`old_value`,`.`new_value`
    having (`Severity_modified` >= (now() - interval 2 week))
    order by bug_id` ASC

例如,对于 id 为 8 的错误,我通过此查询获得了三个条目。 id 为 8 的 bug 在过去 2 周内确实发生了三个严重性变化,但我只想获得最新的严重性变化。

我的查询可能有什么问题?

【问题讨论】:

    标签: mysql group-by max aggregate-functions mantis


    【解决方案1】:

    max() 是一个聚合函数,它似乎不适合您尝试做的事情。

    我感觉您正在尝试做的是从 mantis_bug_history_table 中所有适用的 bug_id 中获取最新信息。如果这是真的,那么我将重写查询如下 - 我将编写一个子查询 getLatest 并将其与 prepared_bug_list 连接

    更新答案

    注意:我无权访问实际的数据库表,因此此查询可能存在错误

    select
        `getLatest`.`last_bug_id`
        , `mantis_bug_history_table`.`date_modified`
        , `mantis_bug_history_table`.`old_value`
        , `mantis_bug_history_table`.`new_value`
    from 
        (
            select
                  (
                    select
                        `bug_id` 
                    from
                        `mantis_bug_history_table` 
                    where 
                        `date_modified` > unix_timestamp() - 14*24*3600 -- two weeks
                        and `field_name` like 'severity'
                        and `bug_id` = `prepared_bug_list`.`bug_id`
                    order by 
                        `date_modified` desc
                    limit 1
                  ) as `last_bug_id`
            from
                `prepared_bug_list` 
        ) as `getLatest`
        inner join `mantis_bug_history_table`
                on `prepared_bug_list`.`bug_id` = `getLatest`.`last_bug_id`     
    order by `getLatest`.`bug_id` ASC
    

    【讨论】:

    • 感谢您的快速回答,但我不明白加入的子查询 (getLatest) 将如何提供帮助。限制 1 表示我将只获得整个查询的总行数(这就是我看到结果的唯一一个错误)。我认为您对子查询的意图是通过按降序对日期进行排序(将最新的日期放在顶部)来获取每个错误的最新结果,并将每个错误的结果限制为 1 个,但它不能按您的预期工作.
    • @Bruder -- 我无权访问您的数据库,所以我无法调试我的查询,只能目不转睛地盯着它。您可以通过取出子查询并查看它得到什么来进行故障排除。也许在您的数据中,过去两周确实只有一个错误?祝你好运! (或者您可以将您的数据和表格放在 SqlFiddle.com 上,我可以进一步调试我的查询。)
    • @Bruder -- 对不起。事实上,我上次查询的逻辑中有一个错误。似乎我们无法避免使用 correlated subquery(这往往很慢)。我已经更新了我的答案。请看看它是否有效并告诉我。
    • @Bruder -- 很遗憾听到我更新的查询不起作用。如果您将表架构(希望也有一些测试数据)放在 SqlFiddle.com 上,我很乐意再试一次。祝你好运!
    • 感谢您的贡献,您对限制 1 的提示是正确的路径。我现在有一个解决方案;我已经把它贴在下面了。
    【解决方案2】:

    我终于有办法了!我的朋友帮助了我,其中一个解决方案是包含 mantis 错误历史表的主键,它不是 bug_id,而是列 id,它是一个连续的数字。 解决方案的另一部分是 where 子句中的子查询:

        select `prepared_bug_list`.`bug_id` AS `bug_id`,
    `mantis_bug_history_table`.`old_value` AS `old_value`,
    `mantis_bug_history_table`.`new_value` AS `new_value`,
    `mantis_bug_history_table`.`type` AS `type`,
    date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %H:%i:%s') AS `date_modified`
     FROM `prepared_bug_list`
     JOIN mantis_import.mantis_bug_history_table
     ON `prepared_bug_list`.`bug_id` = mantis_bug_history_table.bug_id
     where (mantis_bug_history_table.id = -- id = that is the id of every history entry, not confuse with bug_id
        (select `mantis_bug_history_table`.`id` from `mantis_bug_history_table` 
         where ((`mantis_bug_history_table`.`field_name` = 'severity') 
         and (`mantis_bug_history_table`.`bug_id` = `prepared_bug_list`.`bug_id`))
        order by `mantis_bug_history_table`.`date_modified` desc limit 1)
    and `date_modified` > unix_timestamp() - 14*24*3600 ) 
    order by `prepared_bug_list`.`bug_id`,`mantis_bug_history_table`.`date_modified` desc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2023-03-21
      相关资源
      最近更新 更多