Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在Sql中将每6行转换为列1、2、3等的简单方法?_Mysql_Sql - Fatal编程技术网

Mysql 在Sql中将每6行转换为列1、2、3等的简单方法?

Mysql 在Sql中将每6行转换为列1、2、3等的简单方法?,mysql,sql,Mysql,Sql,谢谢你的帮助!我真的不知道怎么做。使用PIVOT将行转换为列 看看例子 没有ID列或类似列?好像你迷路了…这些行是如何分组的?MySQL表未排序。是否可以添加文本数据欢迎使用堆栈溢出!你的文本图像。它不能被大声朗读或复制到编辑器中,而且索引也不太好,这意味着其他有同样问题的用户在这里找到答案的可能性较小。请您的帖子直接包含相关文本(最好使用复制+粘贴以避免抄写错误)。 SELECT IFNULL(empId,'Totals') AS EmpId, -- outer query


谢谢你的帮助!我真的不知道怎么做。

使用PIVOT将行转换为列

看看例子


没有ID列或类似列?好像你迷路了…这些行是如何分组的?MySQL表未排序。是否可以添加文本数据欢迎使用堆栈溢出!你的文本图像。它不能被大声朗读或复制到编辑器中,而且索引也不太好,这意味着其他有同样问题的用户在这里找到答案的可能性较小。请您的帖子直接包含相关文本(最好使用复制+粘贴以避免抄写错误)。
SELECT  
  IFNULL(empId,'Totals') AS EmpId,       -- outer query labels rollup row 
  sums.2005, sums.2006, sums.2007,       -- and calculates horizontal sums 
  sums.2005 + sums.2006 + sums.2007 AS Sums 
FROM (                                   -- inner query groups by employee 
  SELECT                                 -- with an expression for each column 
    EmpID, 
    SUM(IF(Yr=2005,sales,0)) As '2005', 
    SUM(IF(Yr=2006,sales,0)) As '2006', 
    SUM(IF(Yr=2007,sales,0)) As '2007' 
  FROM Sales 
  GROUP BY EmpID WITH ROLLUP 
) AS sums; 

+--------+----------+----------+----------+-----------+ 
| EmpId  | 2005     | 2006     | 2007     | Sums      | 
+--------+----------+----------+----------+-----------+ 
| 1      | 12000.00 | 18000.00 | 25000.00 |  55000.00 | 
| 2      | 15000.00 |  6000.00 |     0.00 |  21000.00 | 
| 3      |     0.00 | 20000.00 | 24000.00 |  44000.00 | 
| Totals | 27000.00 | 44000.00 | 49000.00 | 120000.00 | 
+--------+----------+----------+----------+-----------+