Mysql single query join 3 table and get all the results

前端 未结 3 1677
鱼传尺愫
鱼传尺愫 2021-01-23 02:44

Hi i want to list all the song for the album and i want to list all the artist for individual song see below for example.

1. Song Title 1
   - Artist 1, Artist 2,         


        
相关标签:
3条回答
  • 2021-01-23 02:58
    SELECT s.song_name, a.artist_name, al.album_name
    FROM artist a
    LEFT JOIN song_artist sa ON sa.artist_id = a.id
    LEFT JOIN song s ON s.id = sa.song_id
    LEFT JOIN album al ON al.id = s.album_id
    

    More info on JOIN

    0 讨论(0)
  • 2021-01-23 03:02
    Select s.song_name ,art.artist_name,a.album_name from Song
    join song_artist sa on sa.Song_id = s.Song_id
    join Album a on a.album_id = s.Album_id
    join Artist art on art.id = sa.artist_id
    
    0 讨论(0)
  • 2021-01-23 03:11

    This gets you the songs + artists for album 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
    

    Note that you did answer this question in another spot as well.

    0 讨论(0)
提交回复
热议问题