2010-01-24 84 views
5

我有4個不同的查詢,每個查詢都返回單獨的一組結果。 我需要將查詢結果與使用單個查詢結合起來。在MySQL中結合多個查詢結果(按列)

我的示例查詢是:

1. select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id' 

2. select mtn.* from (meetings as mtn inner join meetings_users as mtnusr on mtn.id=mtnusr.meeting_id) inner join users as usr on usr.id=mtn.assigned_user_id where mtn.assigned_user_id='seed_max_id' 

3. select tsk.* from tasks as tsk inner join users as usr on usr.id=tsk.assigned_user_id where tsk.assigned_user_id='seed_max_id' 

4. select nts.* from (notes as nts inner join accounts as acnts on acnts.id=nts.parent_id) inner join users as usr on usr.id=acnts.assigned_user_id where acnts.assigned_user_id='seed_max_id' 

我嘗試以下方法,但它沒有工作

Combine: SELECT tbl1.*, tbl2.* 
from (select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id') as tbl1 
left outer join 
(select mtn.* from (meetings as mtn inner join meetings_users as mtnusr on mtn.id=mtnusr.meeting_id) inner join users as usr on usr.id=mtn.assigned_user_id where mtn.assigned_user_id='seed_max_id') as tbl2 
using(assigned_user_id) 

我也嘗試右外連接和其他內部連接 我真的卡住如果有人知道解決方案,請幫助。 我需要類似的結果,如How can I join two tables with different number of rows in MySQL?

數據樣本:

從查詢1:

+-------------------------------------------+------------------+- 
| Call Name         | Call Description | 
+-------------------------------------------+------------------+- 
| Discuss Review Process     | NULL    | 
| Get More information on the proposed deal | NULL    | 
| Left a message       | NULL    | 
| Discuss Review Process     | NULL    | 
+-------------------------------------------+------------------+ 

從查詢2:

+-----------------------+----------------------------------------------------------- 
| Meeting Name   | Meeting Description 
+-----------------------+----------------------------------------------------------- 
| Review needs   | Meeting to discuss project plan and hash out the details o 
| Initial discussion | Meeting to discuss project plan and hash out the details o 
| Demo     | Meeting to discuss project plan and hash out the details o 
| Discuss pricing  | Meeting to discuss project plan and hash out the details o 
| Review needs   | Meeting to discuss project plan and hash out the details o 
+-----------------------+----------------------------------------------------------- 

我需要的列組合如下所示:

+-------------------------------------------+------------------+-------------------+-------------------+ 
| Call Name         | Call Description |Meeting Name  |Meeting Description| 
+-------------------------------------------+------------------+-------------------+-------------------+ 
| Discuss Review Process     | NULL    |Review needs  |Meeting to discuss | 
| Get More information on the proposed deal | NULL    |Initial discussion |Meeting to discuss | 
| Left a message       | NULL    |Demo    |Meeting to discuss | 
| NULL         | NULL    |Discuss pricing |Meeting to discuss | 
| NULL          | NULL    |Review needs  |Meeting to discuss | 
+-------------------------------------------+------------------+-------------------+-------------------+ 
+0

您需要說明的表是什麼樣子,結果集應該是什麼樣子,以及如何將數據合併。 – cletus 2010-01-24 12:39:48

+0

我需要知道,有沒有辦法做到這一點? – Imrul 2010-01-24 13:41:14

回答

5

你能做的最好的是一個UNION或UNION ALL,但這要求它們具有相同類型和數量的列。例如:

SELECT 'Customer' AS type, id, name FROM customer 
UNION ALL 
SELECT 'Supplier', id, name FROM supplier 
UNION ALL 
SELECT 'Employee', id, full_name FROM employee 

列名稱不必匹配。第一部分的別名將用於其餘部分。

我還要補充一點,而不是:

select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id' 

你應該刪除不必要的子查詢,只是做:

SELECT c.* 
FROM calls c 
JOIN calls_users cu ONc.id = cu.call_id 
WHERE c.assigned_user_id = 'seed_max_id' 

沒有必要的額外的複雜性和上面絕對有更可讀。

+0

這不能在UNION中完成,因爲我需要組合查詢的列。 – Imrul 2010-01-24 12:25:38

0

我假設你想讓你的例子返回一個單行結合所有這些表中的相應條目。試試這個,告訴我們,如果它的工作:

select * from users as usr 
left outer join (calls as cls 
    inner join calls_users as clsusr 
    on cls.id = clsusr.call_id) 
on usr.id = cls.assigned_user_id 

left outer join (meetings as mtn 
    inner join meetings_users as mtnusr 
    on mtn.id = mtnusr.meeting_id) 
on usr.id = mtn.assigned_user_id 

left outer join tasks as tsk 
on usr.id = tsk.assigned_user_id 

left outer join (notes as nts 
    inner join accounts as acnts 
    on acnts.id=nts.parent_id) 
on usr.id = acnts.assigned_user_id 

where user.id = 'seed_max_id'