2016-06-29 69 views
0

假設我有一個表 - 個人排序一列中兩個列

id | First Name | Last Name 
-----------------------------' 
1 AA    BB 
2 EE    DD 
3 CC    FF 

在這裏,我想在1列中顯示的名字和姓氏。

Such that output would be 
---------------------- 
AA 
BB 
CC 
DD 
EE 
FF 

Can i also display it as 
------------------ 
1 AA 
1 BB 
3 CC 
2 DD 
2 EE 
3 FF 

使用Mysql查詢?

+0

是什麼?你試試?我們在這裏幫助你不爲你工作 – goto

回答

2

您正在尋找union all

select first_name as name from persons union all 
select last_name from persons 
order by name; 

編輯:

對於修訂後的問題:

select id, first_name as name from persons union all 
select id, last_name from persons 
order by name; 
+0

我已經更新了這個問題。你能告訴我這是否可能嗎? –

0

如果您想根據id的結果進行排序。然後,

查詢

select t.name from(
    select id, firstname as name 
    from tblPersons 
    union all 
    select id, lastname as name 
    from tblPersons 
)t 
order by t.id, t.name; 
0
select * from 
(
select first_name from person 

union 

select last_name from person 
) 
t 
order by first_name 
+0

儘管這可能會回答這個問題,但最好包括一些支持性的解釋,爲什麼會回答這個問題 – Mathlight