我想知道哪个播放列表有超过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);
答案 0 :(得分: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;
答案 1 :(得分:1)
尝试: -
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。
答案 2 :(得分: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
答案 3 :(得分: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
答案 4 :(得分:0)
如果可能,请始终使用加入
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;