如何选择多行到多列?

时间:2014-04-30 05:22:53

标签: mysql sql pivot

我有一张这样的表:

id name 
1  a    
2  b    
3  c    
4  d   
5  e    

如果我select name from tableTest where id = 1 or id = 2,我得到了这个:

name
a
b

但我想要一个这样的结果:

name_1   name_2 
a        b

-- 1, 2: Best: the value of id, or (AA, BB..) or (a, b, c,..) or anything

我该怎么做

1 个答案:

答案 0 :(得分:0)

select max(case when id=1 then name end) as name_1,
        max(case when id=2 then name end) as name_2
From test

SQL Fiddle Demo