Advertisement
mikelieman

Untitled

Mar 23rd, 2016
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.63 KB | None | 0 0
  1. BEGIN
  2. DROP TABLE IF EXISTS attendeesWithCustomFields;
  3. SET @SQL = NULL;
  4. SELECT GROUP_CONCAT( DISTINCT
  5.         CONCAT(
  6.             'MIN(CASE WHEN cf.field_name = ''',
  7.             field_name,
  8.             ''' THEN cf.field_value END) AS ',
  9.             field_name
  10.         )
  11.     ) INTO @SQL
  12. FROM custom_fields;
  13. SET @SQL = CONCAT(  'CREATE TABLE attendeesWithCustomFields
  14.                     SELECT a.* , ', @SQL , '
  15.                    FROM attendees a
  16.                    LEFT JOIN custom_fields AS cf
  17.                    ON cf.event_id = a.event_id AND cf.investor_id = a.investor_id
  18.                     GROUP BY a.event_id, a.investor_id ' );
  19. PREPARE stmt FROM @SQL;
  20. EXECUTE stmt;
  21. DEALLOCATE PREPARE stmt;
  22. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement