我不知道如何执行以下查询。我有 3 张 table :
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 个查询(子查询),但据我所知......
谢谢
最佳答案
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;
关于mysql - 查询 : get author of a song and count total of songs for that author,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458601/