0

我不确定如何执行以下查询。我有 3 张桌子:

song (song_id, title, is_draft)
author (author_id, name)
song_author (song_id, author_id, display_order)

有歌曲,一首歌可以有多个作者,一个作者可以写多首歌曲

我想输出特定歌曲的作者以及每个作者写的歌曲总数。

所以,如果我选择song_id : 598,我想要这样的结果集

author_id    name           total songs he wrote
------------------------------------------------
234          Michael Lord   58
589          Lama Turc      12

注意:每个作者写的歌曲总数必须排除歌曲是song.is_draft等于 1 并按song_author.displayorder排序结果集

根据我的研究,我认为我必须做 2 个查询(子查询),但据我所知……

谢谢

4

3 回答 3

1
SELECT  author.author_id, author.name, count(song.song_id)
FROM song, author, song_author,
WHERE song.song_id = $id
AND song.song_id = song_author.song_id
AND song_author.author_id = author.author_id
AND song_author.display_order != 1
GROUP BY song_author.author_id, author.name
ORDER BY author.author_id asc;
于 2013-07-03T21:51:03.537 回答
0

尝试这个..

select b.author_Id, b.name, COUNT(a.Song_Id) as 'total'
FROm song_author as a
INNER JOIN author as b
    ON a.author_id = b.author_id
where a.song_id IN (select s.song_id
                    From songs as s
                    Where s.Song_Id = 598
                    AnD s.is_draft = 1)
GROUP bY b.author_Id, b.name
于 2013-07-03T21:48:33.880 回答
-1

对 SQL 也很陌生,但也许这不是我的想法?这并没有明确说明连接,但我发现它适用于我的目的。也许有人可以让我知道这是否是不好的做法?

Select author.author_id
, author.name
, count(author.author_id) 
from song, author, song_author     
where song.song_id = song_author.song_id 
and author.author_id = song_author.author_id 
group by author.authorid, author.name 
order by author.author_id asc;
于 2013-07-03T21:47:54.913 回答