我希望使用单个查询连接3表获得所有结果

时间:2011-07-14 14:00:51

标签: php mysql

  

可能重复:
  Mysql single query join 3 table and get all the results

您好我想列出该专辑的所有歌曲,我想列出所有艺术家的个别歌曲,例如见下文。

1. Song Title 1
   - Artist 1, Artist 2, Artist 3    note:(all this individual artist have link to there artist page)
2. Song Title 2
   - Artist 1, Artist 2
3. Song Title 3
   - Artist 1, Artist 2, Artist 3

我的表格是歌曲,专辑,艺术家,歌曲_artist

歌曲表

+----+-----------+----------+
| id | song_name | album_id |
+----+-----------+----------+
| 1  | Title 1   | 2        |
| 2  | Title 2   | 2        |
| 3  | Title 3   | 2        |
+----+-----------+----------+

专辑表

+----+------------+
| id | album_name |
+----+------------+
| 1  | Album 1    |
| 2  | Album 2    |
| 3  | Album 3    |
+----+------------+

艺术家表

+----+-------------+
| id | artist_name |
+----+-------------+
| 1  | Artist 1    |
| 2  | Artist 2    |
| 3  | Artist 3    |
+----+-------------+

song_artist表

+--------+--------------+---------+
| id     | song_is      |artist_id|
+--------+--------------+---------+
| 1      | 1            | 1       |
| 2      | 1            | 5       |
| 3      | 1            | 3       |
| 4      | 2            | 3       |
| 5      | 2            | 1       |
| 6      | 3            | 2       |
| 7      | 3            | 1       |
+--------+--------------+---------+

这是我目前的代码。

$id =$_GET['id']; *// Get album id from url*
$query = "SELECT id, song_name, FROM song WHERE album_id = '".$id."'";
$result = mysql_query($query) or die("h".mysql_error());
while( $song = mysql_fetch_assoc($result)){
echo $song['song_name'];
$result1 = mysql_query("SELECT artist.artist_name as artist_name, artist.id as aid
FROM artist
INNER JOIN song_artist
ON artist.id = song_artist.artist_id
WHERE song_artist.song_id = '".$song['id']."' ");
while($row = mysql_fetch_array($result1)){
echo "<a href='".$row['sid']."'>".$row['artist_name']."</a>, ";
}
}

如何编写mysql单一查询以获取php中的所有结果?

提前感谢您的帮助。

詹姆斯。

好的我现在得到了我想要的结果,谢谢每一个人。

$query = "select s.id song_id, s.song_name, group_concat(art.artist_name) artname, a.id
        from song s 
        left outer join album a on a.id = s.album_id
        left outer join song_artist sa on sa.song_id = s.id
        left outer join artist art on art.id = sa.artist_id
        WHERE a.id= '".$id."'
        GROUP BY song_id";

2 个答案:

答案 0 :(得分:1)

这可以获得专辑1的歌曲+艺术家。

SELECT song_name, group_concat(artist_name) 
FROM song 
LEFT JOIN song_artist ON song.id=song_artist.song_id 
LEFT JOIN artist ON song_artist.artist_id=artist.id 
WHERE song.album_id=1

答案 1 :(得分:1)

使用查询:

select s.id song_id, s.song_name, a.album_name, art.artist_name
from song s 
left outer join album a on a.id=s.album_id
left outer join song_artist sa on sa.song_id=s.id
left outer join artist art on art.id=sa.art_id
where <INSERT WHERE-CLAUSE HERE> 

你会得到如下结果:

+--------+-----------+------------+-------------+
| song_id|song_name  | album_name | artist_name |
+--------+-----------+------------+-------------+
| 1      | Title 1   | Album 2    | Artist 1    |
| 1      | Title 1   | Album 2    | Artist 5    |
| 1      | Title 1   | Album 2    | Artist 3    |
| 2      | Title 2   | Album 2    | Artist 3    |
| 2      | Title 2   | Album 2    | Artist 1    |
| 3      | Title 3   | Album 2    | Artist 2    |
| 3      | Title 3   | Album 2    | Artist 1    |
+--------+-----------+------------+-------------+

因此必须迭代每个歌曲id以从结果中获得每首歌曲的显示信息。因为你是用一个查询来做的,所以当一首歌有多位艺术家时,你会得到重复的歌曲和专辑名称,但这是你为简洁而付出的代价。

<强>更新

或者你可以使用artist_name上的GROUP_CONCAT作为Eljakim的建议,如果除了连续的显示列表之外你不会使用艺术家信息。