SQL 函数调用被“HAVING"忽略条款 [英] SQL Function calls ignored with "HAVING" clause
本文介绍了SQL 函数调用被“HAVING"忽略条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于下面的查询,MAX 函数似乎被完全忽略了.MAX函数对查询结果没有影响
For the below query, the MAX function seems to be completely ignored. The MAX function has no effect on the result of the query
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,则一切正常.这完全等效:
Your HAVING
clause is saying: take the maximum of the timestamp. If the maximum is not equal to zero or NULL, then everything is fine. This is exactly equivalent:
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_id
和 event_timestamp
上创建索引:
To optimize performance, create an index on alarm_life_cycle_id
and event_timestamp
:
create index alarm_notification_alci_et on alarm_notification(alarm_life_cycle_id, event_timestamp)
这篇关于SQL 函数调用被“HAVING"忽略条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文