2017-01-02 155 views
-1

我有這個表:由兩個字段合併MySQL表

first_name | last_name | mobile | email 
------------------------------------------------------- 
a   | b   | 123  | 
c   | d   | 456  | 
a   | b   |   | [email protected] 
c   | d   |   | [email protected] 

我想這些表結合起來,並得到報告,如:

first_name | last_name | mobile | email 
------------------------------------------------------- 
a   | b   | 123  | [email protected] 
c   | d   | 456  | [email protected] 

我試着按這樣的:

SELECT * FROM table GROUP BY first_name AND last_name

+0

不清楚。你的問題是什麼 ?請編輯問題並解釋聊天內容與您提供的代碼以及您想要的內容相反。 – kebs

+0

你在說'你要組合這些表',但你只有一個表。你不是指「記錄」嗎? – Loko

回答

3

當分組逗號分開的列。還可以使用max()獲得非分組列的值,以避免得到空字符串

SELECT first_name, last_name, 
     max(mobile) as mobile, max(email) as email 
FROM your_table 
GROUP BY first_name, last_name 
0

試試這個:

Select t1.first_name ,t1.last_name,t1.mobile,t2.email from (select * from table where mobile != '') as t1 inner join (select * from table where email != '') as t2 on t1.first_name = t2.first_name;