Skip to content

Instantly share code, notes, and snippets.

@stephenca
Created June 13, 2012 09:16
Show Gist options
  • Save stephenca/2922979 to your computer and use it in GitHub Desktop.
Save stephenca/2922979 to your computer and use it in GitHub Desktop.
Pivot table query using mysql function GROUP_CONCAT
SELECT
t.email,
GROUP_CONCAT(if(a.attr = 'title', value, NULL)) AS 'Title',
GROUP_CONCAT(if(a.attr = 'forename', value, NULL)) AS 'First Name',
GROUP_CONCAT(if(a.attr = 'surname', value, NULL)) AS 'Last Name'
FROM targets t
JOIN attr_value_pairs a
ON t.attr_value_id = a.id
GROUP BY t.email;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment