0

这个查询的大部分工作除了两件事,一件大事,一个,只要我将第四个表[部门_tbl ]添加到查询中,我就会得到大约 8K 行,而我应该只有大约 100 行。

请参阅附加的架构,没有复选标记,这些是我想要返回的字段。

这无济于事,但这只是我几乎一直在工作的查询之一,直到 [department_tbl 被添加到组合中]

SELECT _n_cust_entity_storeid_15.entity_id, 
    _n_cust_entity_storeid_15.email, 
    customer_group.customer_group_code, 
    departments.`name`, 
    departments.manager, 
    _n_cust_rpt_copy.first_name, 
    _n_cust_rpt_copy.last_name, 
    _n_cust_rpt_copy.last_login_date, 
    _n_cust_rpt_copy.billing_address, 
    _n_cust_rpt_copy.billing_city, 
    _n_cust_rpt_copy.billing_state, 
    _n_cust_rpt_copy.billing_zip
FROM _n_cust_entity_storeid_15 INNER JOIN customer_group ON _n_cust_entity_storeid_15.group_id = customer_group.customer_group_id
     INNER JOIN departments ON _n_cust_entity_storeid_15.store_id = departments.store_id, 
    _n_cust_rpt_copy
ORDER BY _n_cust_rpt_copy.last_name ASC

我已经尝试过子查询、连接,但无法让它工作。

任何帮助将不胜感激。

Schema 注意entity_idcust_id字段将是_ncust_rpt_copy 表_n_cust_entity_storeid_15 tbl之间的链接 在此处输入图像描述

4

1 回答 1

1

你有一个cross join到最后一张桌子,_n_cust_rpt_copy

SELECT _n_cust_entity_storeid_15.entity_id, 
    _n_cust_entity_storeid_15.email, 
    customer_group.customer_group_code, 
    departments.`name`, 
    departments.manager, 
    _n_cust_rpt_copy.first_name, 
    _n_cust_rpt_copy.last_name, 
    _n_cust_rpt_copy.last_login_date, 
    _n_cust_rpt_copy.billing_address, 
    _n_cust_rpt_copy.billing_city, 
    _n_cust_rpt_copy.billing_state, 
    _n_cust_rpt_copy.billing_zip
FROM _n_cust_entity_storeid_15 INNER JOIN
     customer_group
     ON _n_cust_entity_storeid_15.group_id = customer_group.customer_group_id INNER JOIN
     departments
     ON _n_cust_entity_storeid_15.store_id = departments.store_id join
    _n_cust_rpt_copy
     ON ???
ORDER BY _n_cust_rpt_copy.last_name ASC;

对我来说,正确的连接条件是什么并不明显,但必须有一些东西。

我猜他们至少包括部门:

   _n_cust_rpt_copy
     ON _n_cust_rpt_copy.department_name = departments.name and
于 2013-08-08T21:10:05.747 回答