0

我被困在 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_productstbl_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.

4

1 回答 1

0

问题是您的查询返回表 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) 表上存在的记录,因为它们要么来自此表,要么来自与此表上的记录进行 LEFT OUTER JOINed 的表。

第一个查询将返回在 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
于 2014-06-19T08:31:12.253 回答