Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

join - Combine Multiple Query Results in MySQL (by column)

I have 4 different queries and each of them return individual unique set of Results. I need to combine the Query Results with using a single query.

my sample queries are:

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'

I tried the following way, but it didn't work

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)

i also tried right outer join and other inner joins I am really stuck, if any one know the solution then please help. I need the similar result like How can I join two tables with different number of rows in MySQL?.

Data Sample:

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

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

i need to combine the columns like the following:

+-------------------------------------------+------------------+-------------------+-------------------+
| 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 |
+-------------------------------------------+------------------+-------------------+-------------------+
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best you can do is a UNION or UNION ALL but this requires them to have the same type and number of columns. For example:

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

The column names don't have to match. The aliases from the first part will be used for the rest.

I'll also add that instead of:

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'

you should remove the unnecessary subquery and just do:

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

There's no need for the extra complexity and the above is eminently more readable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...