简体   繁体   English

MYSQL 第 1 行的错误 1111 (HY000):组 function 的使用无效。如何修复此错误?

[英]MYSQL ERROR 1111 (HY000) at line 1: Invalid use of group function. How can I fix this error?

select h.hacker_id, name, count(challenge_id) as total from Challenges c inner join Hackers h on h.hacker_id=c.hacker_id group by h.hacker_id, name 
having total not in 
(select count(challenge_id) as cnt from Challenges c where c.hacker_id!=h.hacker_id
group by c.hacker_id
having cnt!= (select max(count(challenge_id)) from Challenges group by hacker_id))
order by total desc, h.hacker_id

Here is my MySql code and I got an error: ERROR 1111 (HY000) at line 1: Invalid use of group function.这是我的 MySql 代码,我收到一个错误:第 1 行的错误 1111 (HY000):组 function 的使用无效。

I don't know what is wrong with this line: (select max(count(challenge_id)) from Challenges group by hacker_id) How can I fix this error?我不知道这一行有什么问题: (select max(count(challenge_id)) from Challenges group by hacker_id)我该如何解决这个错误?

The link of the problem that I want to solve: https://www.hackerrank.com/challenges/challenges/problem我想解决的问题链接: https://www.hackerrank.com/challenges/challenges/problem

You can't have 2 grouping functions together with 1 group by.您不能将 2 个分组函数与 1 个分组依据一起使用。

Instead of代替

select max(count(challenge_id)) from Challenges group by hacker_id)

you can do你可以做

select max(cnt_challenge) from (select count(challenge_id) as cnt_challenge from Challenges group by hacker_id)) 

The problem is the max(count(). However, I would solve it using limit` rather than an additional subquery:问题是max(count(). However, I would solve it using

having cnt <> (select count(*)
               from challenges
               group by hacker_id
               order by count(*) desc
               limit 1
              )

That said, this query is probably better written using window functions.也就是说,使用 window 函数可能会更好地编写此查询。 However, without sample data, desired results, and a clear explanation of what the query should be doing, it is hard to make concrete suggestions.然而,如果没有样本数据、期望的结果以及对查询应该做什么的明确解释,就很难提出具体的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM