将我的sql行转换为列

时间:2017-11-17 10:17:54

标签: mysql mysql-workbench

我有以下结构。 | ID |值|

| A | V1 |

| B | V2 |

| C | V3 |

| d | V4 |

需要输出。

| A | C | Ç| d |

| V1 | V2 | V3 | V4 |

我尝试了很多,但可以获得成功

select case  when t1.id  = 'a' then t1.value end as a ,
         case    when t1.id  = 'b' then t1.value end as b ,
          case   when t1.id  = 'c' then t1.value end as b ,
          case   when t1.id  = 'd' then t1.value end as d 


           from
t1 

请告知。

由于

1 个答案:

答案 0 :(得分:1)

您将聚合函数用于所需的结果集

"Financial summary 1997"
"v4 Minutes 1995"

Demo

  

注意使用不带group by的聚合函数会将整个表视为单个组