简体   繁体   中英

Mysql select multiple artist records per track using a junction table

First off, I'm not sure if it is possible to do this in 1 Query, I do hope so though.

I have these tables:

artist
- id
- name

track
- id
- title

track_artist
- track_id
- artist_id
(with UNIQUE index on the combined columns)

A track can have multiple artists, and an artist can have multiple tracks, thus both of them have a ManyToMany relation using a junction table.

Now I'd like the information about Track #1 including some form of array (as an value within that record, perhaps named artists ) which contains all artist records that are related to that track.

Which in pseudo-code should end up looking something like this:

- id : 1
- title : "example Track Title"
- artists :
    [
     - id : 1
     - name : "Foo Artist Name"
    ],
    [
     - id : 2
     - name : "Bar Artist Name"
    ]

Is this possible? If not, what would be the best approach?

Yes this is possible you can do it using following query

    Select t.id,t.tital , group_concat(a.name) as artist name 
from track t join track_artist ta on t.id=ta.track_id inner join
 artist a on a.id=ta.artist_id group by t.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM