mysql - 查询 : get author of a song and count total of songs for that author

标签 mysql subquery

我不知道如何执行以下查询。我有 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/

相关文章:

sql - "Subquery returned more than 1 value"用于 SELECT 子查询

mysql - Mysql 查询中 Sum 的平均值

mysql - 仅选择数据库中的最新条目

PHP如果 session 等于mysql表行,则执行某些操作

php - 如何访问 joomla 数据库来运行此代码

mysql - 给定时间内每 5 分钟的平均数据

mysql - 通过 MySQL 存储过程加载 XML

mysql - 插入大量日期格式错误的 mysql 行

mysql - 如何在 MySQL 中使用 ORDER BY FIELD 和子查询?

mysql - 使用 NOT IN 子查询返回 0 行的查询