这是当用户从我的网站按注销时运行的简单查询。
UPDATE `user_logins`
SET `active` = 0
WHERE `user_id` = 3
AND `datetime` = MAX(`datetime`) LIMIT 1
user_id
值与 PDO 绑定在其中。
我抛出以下异常
群组功能使用无效
谷歌搜索似乎说这是因为我在 WHERE
子句中使用了聚合函数。
我还在 Stack Overflow 上发现了这个 问题,但正在使用 HAVING 似乎对我不起作用。我尝试用 HAVING
替换 AND
。
如何更改此查询以不使用聚合(或使用 HAVING
),但仍执行相同的功能?
非常感谢!
This is a simple query ran when the user presses logout from my website
UPDATE `user_logins`
SET `active` = 0
WHERE `user_id` = 3
AND `datetime` = MAX(`datetime`) LIMIT 1
The user_id
value is binded in there with PDO.
I get the following exception thrown
Invalid use of group function
Googling around seems to say that it is because I am using an aggregate function in a WHERE
clause.
I also found this question on Stack Overflow, but playing around with HAVING
didn't seem to work for me. I tried replacing the AND
with HAVING
.
How can I change this query to not use the aggregate (or to use HAVING
), but still perform the same functionality?
Thanks a bunch!
发布评论
评论(1)
您可以使用
ORDER BY
和LIMIT
:这会将该用户的最高日期时间放在第一位,而
LIMIT 1
将确保只有如果有多个记录与查询匹配,则更新第一条记录。You could use
ORDER BY
andLIMIT
:That will put the highest date time for that user first, and the
LIMIT 1
will ensure that only the first record gets updated if more than one record matches the query.