o*********b 发帖数: 13 | 1 我自己感觉我做对了 但是面我的烙印非说不对 我把我的答案贴在这里
Given two tables
1. Candidate: Id , Name
2. Vote: Id, CandidateId(this is the id being voted)
Give query to give the name of the winning candidate
我的答案 因为也没有指定具体那个SQL 所以我认为把logic回答出了就可以了
select Name, Votes from (
select c.Name as Name, count(v.Id) as Votes
from Candidate c join Vote v on c.Id = v.CandidateId
group by c.Name
order by COUNT(v.Id) desc )
limit 1 |
h*******3 发帖数: 52 | 2 我试着写了一个,用的是 implicit join
select Name from Candidate c,
(select CandidateId from votes order by count(CandidateId) desc limit 1 )
votes
where c.Id = votes.CandidateId.
会不会看起来更加清楚些。
【在 o*********b 的大作中提到】 : 我自己感觉我做对了 但是面我的烙印非说不对 我把我的答案贴在这里 : Given two tables : 1. Candidate: Id , Name : 2. Vote: Id, CandidateId(this is the id being voted) : Give query to give the name of the winning candidate : 我的答案 因为也没有指定具体那个SQL 所以我认为把logic回答出了就可以了 : select Name, Votes from ( : select c.Name as Name, count(v.Id) as Votes : from Candidate c join Vote v on c.Id = v.CandidateId : group by c.Name
|
o*********b 发帖数: 13 | 3 请问
select CandidateId from votes order by count(CandidateId) desc
count(CandidateId)什么意思? 难道是产生 每个CandidateId的频率统计? 我的理
解是 count(CandidateId)产生CandidateId到底有多少个distinct value?
【在 h*******3 的大作中提到】 : 我试着写了一个,用的是 implicit join : select Name from Candidate c, : (select CandidateId from votes order by count(CandidateId) desc limit 1 ) : votes : where c.Id = votes.CandidateId. : 会不会看起来更加清楚些。
|
h*******3 发帖数: 52 | 4 有个地方需要 clarify 一下,就是 votes 表中一个Id对应多个 CandidateId ,
语法应该是?
select count(id) IDs, CandidateId from votes group by CandidateId order by
IDs desc
【在 o*********b 的大作中提到】 : 请问 : select CandidateId from votes order by count(CandidateId) desc : count(CandidateId)什么意思? 难道是产生 每个CandidateId的频率统计? 我的理 : 解是 count(CandidateId)产生CandidateId到底有多少个distinct value?
|
o*********b 发帖数: 13 | 5 你刚开始的那个答案应该是不对的吧?
但是我的答案没有错误啊
【在 h*******3 的大作中提到】 : 有个地方需要 clarify 一下,就是 votes 表中一个Id对应多个 CandidateId , : 语法应该是? : select count(id) IDs, CandidateId from votes group by CandidateId order by : IDs desc
|
b******u 发帖数: 81 | |