题54:
根据下面两表请编写 sql 语句来找到当选者的名字。

解题思路:
(1)首先根据Vote表找出选票最多的id;
select CandidateId
from Vote
group by CandidateId--根据id分组
order by count(*) desc--根据票数降序排列
limit 1
(2)然后查询的票最多的名字;
(3)(1)是在父查询的筛选条件。
select a.Name as Name
from Candidate as a
where id = (
select CandidateId
from Vote
group by CandidateId
order by count(*) desc
limit 1
) ;
2809

被折叠的 条评论
为什么被折叠?



