= 0,1,0)) , S-6ren">
gpt4 book ai didi

mysql - 收到 "[Err] 1111 - Invalid use of group function"错误

转载 作者:行者123 更新时间:2023-11-29 14:07:36 34 4
gpt4 key购买 nike

我目前正在执行此查询:

SELECT Date_Format(M.Signupdate,"%Y-%m") as YearMonth,
SUM(IF(Count(H.Nr) >= 0,1,0)) ,
SUM(IF(Count(H.Nr) >= 1,1,0)) ,
SUM(IF(Count(H.Nr) >= 2,1,0)) ,
SUM(IF(Count(H.Nr) >= 3,1,0))
FROM people M
INNER JOIN History H ON H.login = M.Login
WHERE
M.Masteraccount = "" AND
M.logincount > 5 AND
DATEDIFF(M.lastlogin,M.Signupdate) >= 3 AND
DATEDIFF(H.EntryDate,M.Signupdate) <= 151
GROUP BY YearMonth ORDER BY YearMonth ;

但是,我不断收到“[Err] 1111 - 组功能使用无效”错误。如果我删除 SUM,它就工作得很好。请问有人可以回答我吗?

最佳答案

您不能在GROUP BY 中使用别名。您需要将 Date_Format(M.Signupdate,"%Y-%m") 放入 GROUP BY 中才能使其正常工作。此外,您不能嵌套聚合函数,因此您需要使用子查询来获取 count,然后使用 sum:

select YearMonth,
SUM(IF(CountNR >= 0,1,0)) ,
SUM(IF(CountNR >= 1,1,0)) ,
SUM(IF(CountNR >= 2,1,0)) ,
SUM(IF(CountNR >= 3,1,0))
from
(
SELECT Date_Format(M.Signupdate,"%Y-%m") as YearMonth,
Count(H.Nr) CountNR
FROM people M
INNER JOIN History H
ON H.login = M.Login
WHERE M.Masteraccount = "" AND
M.logincount > 5 AND
DATEDIFF(M.lastlogin,M.Signupdate) >= 3 AND
DATEDIFF(H.EntryDate,M.Signupdate) <= 151
GROUP BY Date_Format(M.Signupdate,"%Y-%m")
) src
GROUP BY YearMonth
ORDER BY YearMonth;

关于mysql - 收到 "[Err] 1111 - Invalid use of group function"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124365/

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