简单的SQL查询,导致无效使用组函数 [英] simple SQL query giving Invalid use of group function
本文介绍了简单的SQL查询,导致无效使用组函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我为什么我无法有效使用组函数以及如何停止使用它?
Can anyone tell me why I'm getting Invalid use of group function and how to stop it?
SELECT Name, Message
FROM flux_chat_messages
WHERE id >= ( MAX( id ) -5 )
ORDER BY id ASC
推荐答案
您不能在WHERE
中使用MAX()
.因此,将其包装在一个子查询中,例如:
You cannot use MAX()
in a WHERE
. So wrap it in a subquery like:
SELECT Name, Message
FROM flux_chat_messages
WHERE id >= (SELECT MAX( id ) - 5 FROM flux_chat_messages)
ORDER BY id ASC
也许你也可以拥有
SELECT Name, Message
FROM flux_chat_messages
ORDER BY id DESC
LIMIT 5
并反转程序中的结果(或对此使用另一个子查询)
and reverse the results in your program (or use another subquery for that)
这篇关于简单的SQL查询,导致无效使用组函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文