Mysql查询将行转置为列

时间:2015-12-01 08:16:58

标签: mysql

我正在编写一个查询,根据下面显示的表格中的id列将行转置为列

Table1
id  value
80  3
80  
80  3
80  2
80  
80  2
81  6
81  
81  
81  
81  7
81  6
82  
82  
82  3
82  
82  
82  2

我需要的输出如下

id  value1  value2  value3  value4  value5  value6
80  3       null    3       2       null    2
81  6       null    null    null    7       6
82  null    null    3       null    null    2

但我得到的输出如下

id  value1  value2  value3  value4  value5  value6
80  3       3       3       3       3       3
81  6       6       6       6       6       6
82                                          

我的查询如下

SELECT  
id,
IF(`id` = id , rate, rate) as value,
IF(`id` = id , rate, rate) as value,
IF(`id` = id , rate, rate) as value,
IF(`id` = id , rate, rate) as value,
IF(`id` = id , rate, rate) as value,
IF(`id` = id , rate, rate) as value
from
Table1
group by id;

请告知我哪里出错并进行必要的更改以实现此目的

0 个答案:

没有答案