查询将列数据用作列名

时间:2015-04-28 03:58:35

标签: mysql sql ms-access-2013

请帮我创建一个MYSQL查询,将列数据用作输出表中的列名:

这是我的表:

ID | Name | Stage
-------------------
1  | John | Stage 1
2  | Mark | Stage 3
3  | Bill | Stage 2
4  | Mary | Stage 2

I want to see this:

Stage 1 | Stage 2 | Stage 3
---------------------------
John    | Bill    | Mark
        | Mary    |

1 个答案:

答案 0 :(得分:2)

这是你的mysql查询:

select
  case when stage = 'Stage 1' then name end 'Stage 1',
  case when stage = 'Stage 2' then name end 'Stage 2',
  case when stage = 'Stage 3' then name end 'Stage 3'
  from table1;

http://sqlfiddle.com/#!9/1ee5d/1