如何解决MySql:与分组依据不存在跳过重复
我有这个挑战,我想办法解决,但是我不明白为什么会出错。
写一个查询以打印hacker_id,姓名和每个学生创建的挑战总数。按挑战总数按降序对结果进行排序。如果一个以上的学生提出了相同数量的挑战,则按hacker_id对结果进行排序。如果有多个学生创建了相同数量的挑战,并且该数量少于创建的最大挑战数量,则从结果中排除这些学生。
黑客:hacker_id是黑客的ID,名称是黑客的名称。 挑战:Challenge_id是挑战的ID,而hacker_id是创建挑战的学生的ID。
我的解决方案:
select h.hacker_id,h.name,count(c.challenge_id) as total from challenges c
join hackers h
on h.hacker_id= c.hacker_id
where not exists
(select h1.hacker_id,h1.name,count(C1.challenge_id) as total1 from challenges C1
join hackers h1 on h1.hacker_id= c1.hacker_id
group by h1.hacker_id,h1.name
having total1 < max(count(c.challenge_id) and total1 = count(c.challenge_id)
order by total1 desc)
group by h.hacker_id,h.name
order by total desc
我收到此错误: 第1行的错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册以获取正确的语法,以便在'按总顺序1的顺序附近使用)
按h.hacker_id,h.name分组 在第16行按总desc'排序
解决方法
select h.hacker_id,h.name,count(c.challenge_id) as total from challenges c
join hackers h
on h.hacker_id= c.hacker_id
where not exists
(select t.total1
from (select count(C1.challenge_id) as total1
from challenges C1
join hackers h1 on h1.hacker_id = c1.hacker_id
group by h1.hacker_id) t
having t.total1 < big = (select max(count)
from (select count(c3.challenge_id) as count from challenges c3 group by c3.hacker_id))) big
and t.total1 = 1
)
group by h.hacker_id,h.name
order by total desc
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。