gpt4 book ai didi

mysql - 需要重写查询以在 where 子句中使用 max 函数

转载 作者:行者123 更新时间:2023-11-29 11:49:31 25 4
gpt4 key购买 nike

此时使用的查询:

update metrics.time_created 
set cumu_count = (
select count(*)
from perf_stats.time
where date(insert_datetime)='2015-12-18'
)
where id=max(id);

我在这里得到“组函数的无效使用”——我该如何重写它以保持相同的逻辑?我必须更新metrics.time_created 表中的最新行。

最佳答案

试试这个:

update metrics.time_created a
set cumu_count = (
select count(*)
from perf_stats.time
where date(insert_datetime)='2015-12-18'
)
where exists (
select 1
from (select max(id) as maxid from metrics.time_created) t
where maxid = a.id
);

示例演示:http://sqlfiddle.com/#!9/6bc3cd/1

编辑:

根据评论,更改如下

update metrics.time_created a

set cumu_count =
(
select count(*)
from perf_stats.time pt
where exists
(
select 1
from (select max(curr_date) as mcd from metrics.time_created) x
where mcd = date(insert_datetime)
)
)

where exists
(
select 1
from (select max(id) as maxid from metrics.time_created) t
where maxid = a.id
);

示例演示:http://sqlfiddle.com/#!9/fcc91a/1

关于mysql - 需要重写查询以在 where 子句中使用 max 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com