3

我想知道哪个播放列表有超过 2 首歌曲。该语句有效,但我想要播放列表的名称和显示歌曲的 count() 。我想我必须使用连接,但我不明白这应该如何工作。有人可以帮忙吗?

playlist table
++++++++++++++
id
name

playlist_songs table
++++++++++++++++++++
song_id
playlist_id


SELECT p.name FROM playlist p 
WHERE p.id in (SELECT s.playlist_id counter FROM playlist_songs s
group by playlist_id
having count(song_id)>2);
4

5 回答 5

4

我想要播放列表的名称和所显示歌曲的 count()。

这是使用谓词JOIN的优点之一:IN

SELECT 
  p.name, 
  COUNT(song_id) counter
FROM playlist p 
INNER JOIN playlist_songs s ON  p.id = s.playlist_id 
GROUP BY playlist_id
HAVING COUNT(song_id) > 2;
于 2013-01-09T11:22:59.287 回答
2

尝试:-

SELECT
  p.name,
  count(s.song_id)
FROM
  playlist p
INNER JOIN
  playlist_songs s
ON
  p.id = s.playlist_id
GROUP BY
  p.name
HAVING
  COUNT(s.song_id) >= 2

这是 SQL 服务器语法,但应该适用于 Oracle。

于 2013-01-09T11:24:12.950 回答
0

这就是你需要的:

SELECT p.name as name, count(*) as counter
FROM playlist p left outer join playlist_songs s on (p.id = s.playlist_id)
GROUP BY p.name
HAVING count(*) > 2
于 2013-01-09T11:24:43.823 回答
0

请试试:

SELECT a.name
FROM
  playlist a INNER JOIN  playlist_songs b 
  ON a.id = b.playlist_id
GROUP BY a.name
HAVING COUNT(*) >1
于 2013-01-09T11:28:36.983 回答
0

如果可能,请始终使用 join

SELECT p.name play_list,count(s.song_id) total_songs 
FROM playlist p 
INNER JOIN playlist_songs s 
ON p.id = s.playlist_id 
GROUP BY p.name 
HAVING COUNT(s.song_id) >= 2;
于 2013-01-09T11:36:18.060 回答