由于值在
int其中,因此您要将它们设为列名,因此必须将值包装在反引号中
该SQL将看起来像:
max(case when user_id = 1 then score end) as `1`
完整的查询将是:
SET @sql = NULL;SELECt GROUP_CONCAt(DISTINCT CONCAt( 'max(case when user_id = ''', user_id, ''' then score end) AS `', user_id, '`' ) ) INTO @sqlFROM measure2;SET @sql = CONCAt('SELECt inspection_date, ', @sql, ' FROM measure2 GROUP BY inspection_date');PREPARE stmt FROM @sql;EXECUTE stmt;DEALLOCATE PREPARE stmt;
参见带有演示的SQL Fiddle
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)