开发者

MySQL LEFT JOIN multiple tables logic problem

I have 3 tables: singers, songs, albums. They are all linked with singer_id

I need to get all of them even if there is no matching singer_id. I managed to get all of them with this query:

   SELECT singers.singer_name, albums.album_name, songs.song_name 
     FROM singers
LEFT JOIN albums ON singers.singer_id = albums.singer_id 
LEFT JOIN songs ON albums.singer_id = songs.singer_id
    WHERE singer_id = ?

But the problem is I can;t display the results properly.

Let's say the singer have 3 albums and 11 songs. This is how i display them...

Displaying the singer name:

echo $results[0]['singer_name']

Displaying the songs names:

foreach($results as $song) {
   // PROBLEM: instead of getting 11 songs i'm getting 33 results so each song show up 3 times.
   echo $song['song_name'] 
}

Displaying the albums:

foreach($results as $album) {
 // PROBLEM: i'm getting 33 albums instead of 3 each album shows up 3 times.
  echo $album['album_name'] 
}

EDIT: the songs table have a reference to the albums table album_id but there are songs without an album yet so i need to get the results just by the singer_id

what i need to get is: 11 songs. 3 albums. 1 singer.

thanks in advance.

EDIT: this solution worked for me. what i wanted is to get all songs for the specified singer id and albums the results i wanted looks like thi开发者_Go百科s.

singer_name : song_name     : album_name
...............................................................
Jay Z       : 99 Problems   : NULL
Jay Z       : Gotta Have It : Watch the Throne

so i wanted to get all the singer songs even if they are not in any album yet.

this is the query that i used.

SELECT singers.singer_name, songs.song_name, albums.album_name
FROM singers
LEFT JOIN songs ON singers.singer_id = songs.singer_id
LEFT JOIN albums ON albums.album_id = songs.album_id
WHERE singers.singer_id = ?

thanks to @knittl i'm getting the results i need.

but there is another problem now. i have 3 albums and 11 songs

the songs display correctly but when i make a foreach loop on the albums i get 33 albums again....

how can i display the albums correctly? i have just 3 albums.


you want all songs. select from songs and perform a left join to get potential data from other tables (order by to get nicer representation):

SELECT a.artist_name, COALESCE(b.album_name, '(no album)'), s.song_name, 
FROM songs s
LEFT JOIN artists a
ON s.singer_id = a.singer_id
LEFT JOIN albums b
ON s.album_id = b.album_id AND s.singer_id = b.singer_id
ORDER BY a.artist_name, b.album_id


The main thing you want are songs, a song must have an artist but may have/or not have an album :

SO :

SELECT     singers.singer_name, IF('' == albums.album_name, 'single', albums.album_name) AS album_name, songs.song_name 
FROM       songs   AS songs
RIGHT JOIN singers AS singers ON (singers.singer_id = songs.singer_id)
LEFT JOIN  albums  AS albums  ON (albums.singer_id = songs.singer_id)
WHERE      songs.singer_id = ?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜