MySQL- How to extract data from one column that combined all of columns in table

时间:2016-01-06 13:48:31

标签: mysql

I want to extract data from one column that combined all of columns in table. and It works on the SELECT method .

ID   FirstName   LastName
1      John        Doe
2      Bugs        Bunny
3      Kim        Johnson

How do i do to show result as:

ALIAS
1
2
3
John
Bugs
Kim
Doe
Bunny
Johnson

thanks for helping me

3 个答案:

答案 0 :(得分:0)

您需要UNION(或UNION ALL)查询:

select ID as ALIAS from tablename
union all
select FirstName as ALIAS from tablename
union all
select LastName as ALIAS from tablename

UNION查询将返回不同的值,UNION ALL将返回所有值。如果您想保留订单,则需要额外的列:

select ALIAS from
(
  select id, ID as ALIAS, 1 as col from tablename
  union all
  select id, FirstName, 2 as col from tablename
  union all
  select id, LastName, 3 as col from tablename
) s
order by col, id

答案 1 :(得分:0)

您可以执行以下操作来实现您的需求:

SELECT ID FROM YourTable
UNION ALL
SELECT FirstName FROM YourTable
UNION ALL
SELECT LastName FROM YourTable

答案 2 :(得分:0)

认为这样做会:

SELECT ID from TableName
UNION ALL
SELECT FirstName from TableName
UNION ALL
SELECT LastName from TableName;

...您需要将TableName替换为您从中获取数据的表的名称。