MySQL or PHP Transform rows to columns

巧了我就是萌 提交于 2019-12-07 09:55:30

If you want to have seperate columns for your years too, you have to add the year (calculated from your column date) to your dynamic sql code:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN
  SET group_concat_max_len=2048;
  SET @sql = NULL;

  SELECT GROUP_CONCAT(DISTINCT CONCAT(
      'MAX(IF(month = ''',
      month,
      ''' and year(date) = ',
      year(date),
      ', amount, NULL)) AS `',
      month,
      '_',
      year(date),
      '`'
    )
    order by date
  ) INTO @sql
  FROM tmp_results;

  if coalesce(@sql,'') != '' then
    set @sql = concat(', ', @sql);
  end if; 

  SET @sql = CONCAT(
    'SELECT r.account, 
     r.region ',  
     coalesce(@sql,''),
    ' FROM tmp_results r
     LEFT JOIN accounts AS a
     on r.account_id = a.id
     GROUP BY r.account, r.region');

  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END

The columns will be named like January_2017, and I added an order by date, otherwise they would usually be unordered.

I added a group by r.region, otherwise it will not work if only_full_group_by is enabled on your server (which is the default value starting with MySQL 5.7).

And I added a test for empty tables (which would otherwise result in an error). If you don't need it and copy only parts of my code into yours, be aware of the missing comma after r.region in SET @sql = CONCAT('SELECT r.account, r.region ' compared to your code, you might have to add it again.

Since the code for each month has a length of about 80, you might have to increase group_concat_max_len to fit your largest possible query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!