This is a problem I often run into, and my solution has always to run numerous queries, but I feel there must be a better way. Here are my two tables for this example:
Artists
id
nameSongs
id
artistID
name
genre
The table are linked as such:
artists.id <-> songs.artistID
I'd like to fetch a list of all artists and at the same time fetch a list of all songs per artist. I'd like to avoid duplicate artist names in my result set, so that I don't have to clean the data set in PHP. Here's an example of the format I'd like the data to be in:
array(0 => array('name' => 'Artist A'
, 'songs' => array(0 => array('name' => 'Song 1'
, 'genre' => 'rock'
)
)
1 => array('name' => 'Song 2'
, 'genre' => 'rock'
)
)
)
1 => array('name' => 'Artist B'
, 'songs' => array(0 => array('name' => 'Song 3'
, 'genre' => 'rap'
)
)
1 => array('name' => 'Song 4'
, 'genre' => 'rap'
)
)
)
)
(I know this isn't a MySQL format, but it's the closest way I could explain)
Is this possible? If so, how?
Thanks!