在MySQL中合并多个查询结果(按列) [英] Combine Multiple Query Results in MySQL (by column)

查看:386
本文介绍了在MySQL中合并多个查询结果(按列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个不同的查询,每个查询都返回一组唯一的结果. 我需要将查询结果与使用单个查询结合起来.

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.

我的示例查询是:

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?.

数据样本:

从查询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 |
+-------------------------------------------+------------------+-------------------+-------------------+

推荐答案

您能做的最好的是UNION或UNION ALL,但这要求它们具有相同的类型和列数.例如:

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.

这篇关于在MySQL中合并多个查询结果(按列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆