gpt4 book ai didi

mysql - 如何修复此 MySQL 查询?

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

SELECT 
avg(con_hits) as avg_hits
FROM
content
WHERE
con_type = 1
AND con_posttime < $twelve_hrs_ago
AND con_refresh = 0
ORDER BY
con_posttime DESC
LIMIT 100

我希望它转到至少 12 小时前发布的第一条记录(由具有合适时间戳的 $twelve_hrs_ago 变量表示),并取 con_hits 列,用于接下来的 100 条记录。在我的示例中,它忽略了 LIMIT,并取表中每条记录的平均值。

有没有办法绕过它?

最佳答案

LIMIT 应用于结果集,在 AVG 计算之后。你可以用子选择做你想做的事:

SELECT avg(con_hits) as avg_hits
FROM (
SELECT con_hits
FROM content
WHERE
con_type = 1
AND con_posttime < $twelve_hrs_ago
AND con_refresh = 0
ORDER BY con_posttime DESC
LIMIT 100
) x;

您也可以使用数据库来计算时间偏移量。将上面的 $twelve_hrs_ago 替换为:

date_add(now(), interval -12 hour)

关于mysql - 如何修复此 MySQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/331770/

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