We have two tables of interest in this query.
First off we have a table of music tracks, each with a unique PK and also a FK pointing towards the album that the track belongs to. The second table is this Album table.
So we have around 10 rows of Tracks per 1 row of Album.
I am trying to aggregate all the tracks belonging to one album into one row, plus the album title (which is held in Albums). This is to avoid fiddly PHP code.
At the moment we have:
SELECT Album.Image_URL, Track.Track_Title, Track.Price
FROM Album, Track, Artist
WHERE (Album.Album_ID=Track.Album_ID) AND (Artist.Artist_ID=Track.Artist_ID)
ORDER BY Album.Album_Name ASC
This is one track per row, each with the correct album info. However I’d like of this albums tracks placed into one row.
Appreciate any help,
A
The problem is that the number of tracks will vary, so you do not know ahead of time how many columns your result set should return.
If you are willing to accept that the max number of tracks is 10, then it is possible to make an ugly query which will return 10 tracks, and some of them may be NULL if there are less than 10 for a given album.
But you might as well just return one row per album/track combo, and then build your data by iterating through the rows returned.
Cheers,
Daniel