MySQL pivot table query with dynamic columns

后端 未结 3 1580
一个人的身影
一个人的身影 2020-11-21 22:12

I\'m using the following tables for storing product data:

mysql> SELECT * FROM product;
+---------------+---------------+--------+
| id | name     | descr         


        
相关标签:
3条回答
  • 2020-11-21 22:51

    Here's stored procedure, which will generate the table based on data from one table and column and data from other table and column.

    The function 'sum(if(col = value, 1,0)) as value ' is used. You can choose from different functions like MAX(if()) etc.

    delimiter //
    
      create procedure myPivot(
        in tableA varchar(255),
        in columnA varchar(255),
        in tableB varchar(255),
        in columnB varchar(255)
    )
    begin
      set @sql = NULL;
        set @sql = CONCAT('select group_concat(distinct concat(
                \'SUM(IF(', 
            columnA, 
            ' = \'\'\',',
            columnA,
            ',\'\'\', 1, 0)) AS \'\'\',',
            columnA, 
                ',\'\'\'\') separator \', \') from ',
            tableA, ' into @sql');
        -- select @sql;
    
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
    
        DEALLOCATE PREPARE stmt;
        -- select @sql;
    
        SET @sql = CONCAT('SELECT p.', 
            columnB, 
            ', ', 
            @sql, 
            ' FROM ', tableB, ' p GROUP BY p.',
            columnB,'');
    
        -- select @sql;
    
        /* */
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        /* */
        DEALLOCATE PREPARE stmt;
    end//
    
    delimiter ;
    
    0 讨论(0)
  • 2020-11-21 22:58

    The only way in MySQL to do this dynamically is with Prepared statements. Here is a good article about them:

    Dynamic pivot tables (transform rows to columns)

    Your code would look like this:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'MAX(IF(pa.fieldname = ''',
          fieldname,
          ''', pa.fieldvalue, NULL)) AS ',
          fieldname
        )
      ) INTO @sql
    FROM product_additional;
    
    SET @sql = CONCAT('SELECT p.id
                        , p.name
                        , p.description, ', @sql, ' 
                       FROM product p
                       LEFT JOIN product_additional AS pa 
                        ON p.id = pa.id
                       GROUP BY p.id');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See Demo

    NOTE: GROUP_CONCAT function has a limit of 1024 characters. See parameter group_concat_max_len

    0 讨论(0)
  • 2020-11-21 23:02

    I have a slightly different way of doing this than the accepted answer. This way you can avoid using GROUP_CONCAT which has a limit of 1024 characters and will not work if you have a lot of fields.

    SET @sql = '';
    SELECT
        @sql := CONCAT(@sql,if(@sql='','',', '),temp.output)
    FROM
    (
        SELECT
          DISTINCT
            CONCAT(
             'MAX(IF(pa.fieldname = ''',
              fieldname,
              ''', pa.fieldvalue, NULL)) AS ',
              fieldname
            ) as output
        FROM
            product_additional
    ) as temp;
    
    SET @sql = CONCAT('SELECT p.id
                        , p.name
                        , p.description, ', @sql, ' 
                       FROM product p
                       LEFT JOIN product_additional AS pa 
                        ON p.id = pa.id
                       GROUP BY p.id');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    0 讨论(0)
提交回复
热议问题