gpt4 book ai didi

mysql - 使用 "HAVING"子句忽略 SQL 函数调用

转载 作者:行者123 更新时间:2023-11-30 23:58:09 25 4
gpt4 key购买 nike

对于下面的查询,MAX 函数似乎被完全忽略了。 MAX函数对查询结果没有影响

SELECT alarm_severity_id
FROM alarm_notification
WHERE alarm_life_cycle_id = 25
having MAX(event_timestamp);

最佳答案

这是您的查询:

SELECT alarm_severity_id
FROM alarm_notification
WHERE alarm_life_cycle_id = 25
HAVING MAX(event_timestamp);

您的 HAVING 子句是说:取时间戳的最大值。如果最大值不等于零或 NULL,那么一切都很好。这是完全等价的:

HAVING MAX(event_timestamp) <> 0

我怀疑你想要:

SELECT alarm_severity_id
FROM alarm_notification
WHERE alarm_life_cycle_id = 25
ORDER BY event_timestamp DESC
LIMIT 1;

编辑:

要优化性能,请在 alarm_life_cycle_idevent_timestamp 上创建索引:

create index alarm_notification_alci_et on alarm_notification(alarm_life_cycle_id, event_timestamp)

关于mysql - 使用 "HAVING"子句忽略 SQL 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23611459/

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