MySQL行转列

IF(expr1,expr2,expr3)

如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值


SET @sql = NULL;

SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(c.label = ''',
      c.label,
      ''', c.value, 0)) AS ''',
      c.label, ''''
    )
  ) INTO @sql
FROM table_name c;

SET @sql = CONCAT('Select phone, ', @sql, 
                        ' From table_name  c');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;


posted @ 2018-02-28 15:46  清泉白石  阅读(302)  评论(0编辑  收藏  举报