嗨,我需要幫助與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