1
select h.hacker_id,h.name ,count(*) as cc from hackers h
inner join challenges s
on h.hacker_id=s.hacker_id
group by h.hacker_id,h.name
having cc=(select count(*) from challenges c
group by c.hacker_id
order by count(*) desc
limit 1
)
or
cc in
(
select total from (select count(*) as total from challenges c
group by c.hacker_id
)counts
group by total
having count(*)=1
)
order by cc desc ,hacker_id asc
2
select h.hacker_id,h.name ,count(*) as cc from hackers h
inner join challenges s
on h.hacker_id=s.hacker_id
group by h.hacker_id,h.name
having cc=(select count(*) from challenges c
group by c.hacker_id
order by count(*) desc
limit 1
)
or
count(*) in
(
select total from (select count(*) as total from challenges c
group by c.hacker_id
)counts
group by total
having count(*)=1
)
order by cc desc ,hacker_id asc
在我写cc而不是计数的第一个中它起作用 但是在第二次查询中,子查询不能给出正确的结果。它不与计数一起使用。 我不知道为什么mysql表现出这种方式 两个查询在ms sql server中都能正常工作 但是在mysql中有一个我讨论过的问题