我想显示总统,副总统和秘书的最高票数,但这个查询最小的票数也会显示,我该怎么办?
select c.picturename, c.candidate_id, v.position, count(v.candidate_id) as count
from votes as v
left outer join candidates as c on c.candidate_id = v.candidate_id
group by v.candidate_id
order by count;
答案 0 :(得分:0)
select *
from (
select position
, candidate_id
, count(*)
from votes
where position = 'president'
group by
position
, candidate_id
order by
count(*) desc
limit 1
)
union all
select *
from (
select position
, candidate_id
, count(*)
from votes
where position = 'vice president'
group by
position
, candidate_id
order by
count(*) desc
limit 1
)
union all
select *
from (
select position
, candidate_id
, count(*)
from votes
where position = 'secretary'
group by
position
, candidate_id
order by
count(*) desc
limit 1
)