嗨,我需要帮助与MySQL。Mysql计数艺术家总歌曲
我拖表艺术家和song_artist现在我想对于艺术家总歌曲。
**Artist** table
**id** | **artist_name**
1 | Artist Name1
2 | Artist Name2
3 | Artist Name3
**song_artist** table
id | song_id | artist_id
1 | 2 | 6
2 | 3 | 5
3 | 2 | 7
4 | 4 | 6
5 | 6 | 8
现在我想的出来把这样
Artist1 (2 songs)
Artist2 (1 songs)
Artist3 (5 songs)
投入2查询现在我得到了这一点,但我想用一个查询来获取所有的细节
这是我目前的代码。
$query = "select id, artist_name from artist order by artist_name";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
$query2 = "select COUNT(artist_id) as total_songs from song_artist WHERE artist_id = '".$row['id']."'";
$result2=mysql_query($query2);
echo $row['artist_name']." (".$row2['total_songs'].")<br>";
}
请帮我解决这个问题。
我刚刚注意到没有一个来自song_artist的artist_ids在Artist表格的ID中! – RolandoMySQLDBA