BKWLD

 

My new favorite MySQL function

By Robert on February 27, 2008 at 5:07 pm

Check out this query:

SELECT
	games.title AS game,
	GROUP_CONCAT(DISTINCT artists.name ORDER BY artists.name ASC SEPARATOR ', ') AS artists
FROM games
INNER JOIN tracks ON tracks.game_id = games.id
INNER JOIN artists ON artists.id = tracks.artist_id
GROUP BY games.id

I want to get a list of the soundtracks of all the games. The list should show me the name of the game and all of the artists that contributed to the soundtrack. I didn’t want to do two queries to select first the games and then the artists. This GROUP_CONCAT function saves the day. When you’re doing a grouping you can use it to join the columns that were collapsed with some separator, like they were an array! Nice!

No Comments »

No comments yet.


RSS feed for comments on this post. | TrackBack URI

Leave a comment