嗨,我是初学者,我希望显示歌曲名称在歌曲表中拥有最多歌曲数量,但我没有这样做,因为子查询一次不能返回两个值。我怎么解决这个问题。下面的代码显示了此错误 - >当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。
这是我的代码 - >
SELECT Singer_Name
FROM Singer
WHERE Singer_id IN(SELECT TOP 1 Singer.Singer_id,COUNT(SongTitle) TotalSounds
FROM Singer,Songs
WHERE Songs.Singer_id=Singer.Singer_id
GROUP BY Singer.Singer_id
ORDER BY TotalSounds DESC)
答案 0 :(得分:1)
SELECT TOP 1 n.Singer_Name, count(*) as Song_Count
FROM Songs s
INNER JOIN Singer n on n.Singer_id = s.Singer_id
GROUP BY n.Singer_id, n.Singer_Name
ORDER BY count(*) DESC
我将n.Singer_id添加到小组中,因为两个歌手可能有相同的名字。
我希望这会有所帮助。
答案 1 :(得分:0)
您可以使用 group by 语句,如下所示:
SELECT Singer_Name, count(SongTitle) c FROM Singer
join Song on Song.singer_id = Singer.singer_id
group by Singer_Name
ORDER BY c desc limit 1;
答案 2 :(得分:0)
您接近可行的解决方案。问题是你在where子句中做了子查询,尝试以这种方式限制你的结果,你可以只返回完整的名单和歌曲数量列表,然后在按计数排序后选择最上面的一个:
TruckNumberMetadataProvider
如果你有第一名的并列,并希望返回两个名字,你可以使它SELECT TOP 1 SingerName FROM (SELECT Singer.Singer_Name, count(1) as TotalSounds
FROM Singer
JOIN Songs
ON Songs.Singer_id=Singer.Singer_id
GROUP BY Singer.Singer_Name, Singer.Singer_id) ss
ORDER BY TotalSounds DESC
,否则它将抓住第一个,任意按照它们出现在表格中的顺序打破平局(可能作者:Singer_id)