我被困在 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
表上进行外部连接。我在outer join
这里尝试过联合。当我搜索并发现它MySql do not support direct outer join
就像其他大型数据库处理程序一样。
如果我在使用联合或任何逻辑时犯了任何错误,请帮助我实现这一目标..
已编辑问题:有7,734tbl_received_sms
条记录,tbl_cust_products
有 3 条记录。所以我需要总共 7737 条记录。如果我UNION
只使用我会得到 3 条记录,如果我使用UNION ALL
我会得到 7737 条记录,但所有记录的所有字段都是NULL
.