2010-01-24 32 views
5

Tôi có 4 truy vấn khác nhau và mỗi truy vấn trả về từng tập hợp kết quả riêng lẻ. Tôi cần phải kết hợp Kết quả Truy vấn bằng cách sử dụng một truy vấn.Kết hợp nhiều kết quả truy vấn trong MySQL (theo cột)

truy vấn mẫu của tôi là:

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' 

Tôi đã thử các cách sau đây, nhưng nó đã không làm việc

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) 

tôi cũng đã cố gắng ngay bên ngoài tham gia và các bên tham gia Tôi thực sự bị mắc kẹt , nếu có ai biết giải pháp thì hãy giúp đỡ. Tôi cần kết quả tương tự như How can I join two tables with different number of rows in MySQL?.

mẫu

dữ liệu:

Từ Query 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    | 
+-------------------------------------------+------------------+ 

Từ Query 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 
+-----------------------+----------------------------------------------------------- 

tôi cần phải kết hợp các cột như sau:

+-------------------------------------------+------------------+-------------------+-------------------+ 
| 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

Bạn cần mô tả bảng trông như thế nào, bộ kết quả sẽ như thế nào và cách kết hợp dữ liệu. – cletus

+0

Tôi cần biết, có cách nào để thực hiện việc này không? – Imrul

Trả lời

5

Điều tốt nhất bạn có thể làm là UNION hoặc UNION ALL nhưng điều này đòi hỏi họ phải có cùng loại và số cột. Ví dụ:

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 

Tên cột không khớp. Các bí danh từ phần đầu tiên sẽ được sử dụng cho phần còn lại.

Tôi cũng sẽ thêm rằng thay vì:

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' 

bạn nên loại bỏ các subquery không cần thiết và chỉ làm:

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

Không cần cho sự phức tạp thêm và là trên eminently hơn có thể đọc được.

+0

Điều này không thể thực hiện được trong UNION, vì tôi cần kết hợp các cột của các truy vấn. – Imrul

0

Tôi giả sử bạn muốn ví dụ của mình trả lại một hàng kết hợp các mục nhập tương ứng từ tất cả các bảng này. Hãy thử điều này và cho chúng tôi biết nếu nó hoạt động:

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' 
Các vấn đề liên quan