【问题标题】:Take maximum time during update left join mysql在更新左加入mysql期间花费最长时间
【发布时间】:2021-12-25 06:26:04
【问题描述】:

我正在更新 ID 和接收者的左连接。

Left table
Id | Receiver | Order Time | guid | item
12 | abc      | 12:35      | NULL | NULL


Right table
Id | Receiver | Time taken | guid  | item
12 | abc      | 50seconds  | yuhb2 | 0
12 | abc      | 189seconds | vef3  | 2

如何进行查询以选择最长耗时的记录?

update tableA a left join TableB b
ON  a.ID=b.ID
AND    a.receiver = b.receiver
set
a.items= b.items
where a.guid is null;

预期输出:

Left table
Id | Receiver | Order Time | guid | item
12 | abc      | 12:35      | vef3 |  2

【问题讨论】:

  • 在您的右表中,您应该有一个主键列,另一列供左表参考(即您显示的 Id 列)

标签: mysql sql sql-update left-join


【解决方案1】:

我在 right_table 中添加了另一列 (id_primary)。 要获得最大值,请在末尾使用此子查询 (select id_primary from right_table order by time_taken desc limit 1)

 update left_table a left join right_table b
    ON  a.id= b.id
    set
    a.item= b.item,
    a.guid= b.guid
    where a.guid is null
    and b.id_primary = (select id_primary from right_table order by time_taken desc limit 1 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2019-11-22
    • 2012-02-18
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多