[英]Full Outer Join with multiple tables in mysql
我被困在1个查询中。我想显示客户的所有产品以及系统在1个网格/行中收到的所有短信。 我可以实现这一点,但事情是只显示客户产品,我需要连接3 4个其他表并显示所有数据,例如产品型号,客户名称等。这其他事情来自其他表。所以我需要2个表来做外连接,并显示4 5个表中的数据。 我已经尝试过,但是失败了。
Select tcp.*
, concat(tc.firstname,' ',tc.lastname) as cust_id
, tc.mobile
, tb.brand_name as brand
, tgt.gadget_type as gadget_type
, tm.model_name as model
, ttt.ticket_type as ticket_type
, trs.registration_source as registration_source
From tbl_cust_products tcp
Left Join `tbl_received_sms` trsm on tcp.id = trsm.cust_prod_id
Left Join tbl_customer tc on tcp.cust_id=tc.id
Left Join tbl_brand tb on tcp.brand = tb.id
Left Join tbl_gadget_type tgt on tcp.gadget_type=tgt.id
Left Join tbl_model tm on tcp.model = tm.id
Left Join tbl_ticket_type ttt on tcp.ticket_type=ttt.id
Left Join tbl_registration_source trs on trs.id=tcp.registration_source
Where tcp.del_date is NULL
Union
Select tcp.*
, concat(tc.firstname,' ',tc.lastname) as cust_id
, tc.mobile
, tb.brand_name as brand
, tgt.gadget_type as gadget_type
, tm.model_name as model
, ttt.ticket_type as ticket_type
, trs.registration_source as registration_source
From tbl_cust_products tcp
Right Join `tbl_received_sms` trsm on tcp.id=trsm.cust_prod_id
Left Join tbl_customer tc on tcp.cust_id=tc.id
Left Join tbl_brand tb on tcp.brand=tb.id
Left Join tbl_gadget_type tgt on tcp.gadget_type=tgt.id
Left Join tbl_model tm on tcp.model = tm.id
Left Join tbl_ticket_type ttt on tcp.ticket_type=ttt.id
Left Join tbl_registration_source trs on trs.id=tcp.registration_source
Where tcp.del_date is NULL
在上面,我只希望在tbl_cust_products
和tbl_received_sms
表上进行tbl_cust_products
tbl_received_sms
。 我在这里尝试了outer join
联接的联盟。 当我进行搜索时发现MySql do not support direct outer join
不像其他大型数据库处理程序那样MySql do not support direct outer join
联接。
如果我在使用联合或任何逻辑上犯任何错误,请帮助我实现这一目标。
编辑的问题:在tbl_received_sms
有7,734条记录,在tbl_cust_products
有3条记录。因此,我总共需要7737条记录。 如果仅使用UNION
我将获得3条记录;如果使用UNION ALL
,则将获得7737条记录,但是所有记录的所有字段均为NULL
。
问题是查询从表tcp(tbl_cust_products),tc(tbl_customer),tb(tbl_brand),tgt(tbl_gadget_type),tm(tbl_model),ttt(tbl_ticket_type)和trs(tbl_registration_source)返回列。
所有这些列都依赖于tcp(tbl_cust_products)表上存在的一条记录,因为它们要么来自此表,要么来自与该表上的记录保持左连接的表。
第一个查询将返回在tcp上具有匹配记录(tbl_cust_products)的任何行。 第二个查询还将返回在trsm(tbl_received_sms)上具有匹配记录的所有查询。 但是,由两者返回的任何内容都将被UNION消除一次。
进一步的问题是,从第二个查询返回的任何行(在tcp上没有匹配记录(tbl_cust_products))都将在查询部分返回的所有字段中都为NULL(因为所有字段都取决于tcp上的匹配项( tbl_cust_products))。 然后,UNION将消除所有这些行中的仅一行,因为它将消除重复项,并且所有行都是相同的(即,所有NULL)。
如果要从中获取输出,则将trsm中的列(tbl_received_sms)添加到返回的列中。 trsm.cust_prod_id可能是一个不错的尝试。
编辑更多细节以解释联合。
以您的查询的简化版本为例:
SELECT tcp.id,
tc.name
FROM tbl_cust_products tcp
LEFT JOIN tbl_received_sms trsm ON tcp.id = trsm.cust_prod_id
LEFT JOIN tbl_customer tc ON tcp.cust_id=tc.id
UNION
SELECT tcp.id,
CONCAT(tc.firstname,' ',tc.lastname) as cust_id
FROM tbl_cust_products tcp
RIGHT JOIN tbl_received_sms trsm ON tcp.id = trsm.cust_prod_id
LEFT JOIN tbl_customer tc ON tcp.cust_id=tc.id
说表包含以下内容
tbl_cust_products
id name cust_id
1 a 5
2 b 6
tbl_received_sms
id cust_prod_id data
3 2 c
4 3 d
5 4 e
tbl_customer
id name
5 fred
6 burt
第一个查询将从tbl_cust_products返回两条记录,其中一条与tbl_received_sms相匹配:
id name
1 fred
2 burt
第二个查询将从tbl_received_sms中找到3条记录,其中一条与tbl_cust_products相匹配。 不匹配的记录在两个返回的字段中都为NULL(因为tbl_cust_products上没有匹配的记录,因此该字段中的字段值为null,tbl_customer中的字段值也相同,这将匹配tbl_cust_products中不存在的记录)。 匹配的记录将被填充:
id name
NULL NULL
NULL NULL
2 burt
UNION会将这两个批次合并在一起,
id name
1 fred
2 burt
NULL NULL
NULL NULL
2 burt
但消除重复项,因此:-
id name
1 fred
2 burt
NULL NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.