MySQL - 加入表已加入表

时间:2017-02-01 17:00:42

标签: mysql join subquery

以下是表格布局的视觉效果,如果您看下面,我的目标是将wn_writing_number_cstm.id_c字段加入preps_contracted_reps.id

为了澄清,你在这里看到的必须保持不变,我需要保持连接的位置,但另外,抓住签约代表的写号。(我需要获得title_c字段。)

这是我当前的代码,我尝试了多个连接,但似乎无法找出正确的JOIN来获得我需要的东西。除了title_c字段之外,这个语句得到了我需要的一切,因为它与代表的写号相关。

编辑:一旦我用别名添加了额外的JOIN语句,它现在什么也没有带回来。

SELECT
leads.id,
leads.date_entered,
leads.date_modified,
leads.first_name,
leads.last_name,
leads.primary_address_state,
leads.converted,
leads.`status`,
leads.lead_source,
leads_cstm.territory_c,
leads_cstm.contact_status_c,
leads_cstm.campaign_id_c,
leads_cstm.industry_c,
leads_cstm.contract_type_c,
leads_cstm.recruiting_link_sent_c,
preps_contracted_reps.first_name AS rep_first_name,
preps_contracted_reps.last_name AS rep_last_name,
wn_writing_number_cstm.title_c
FROM
leads_wn_writing_number_1_c
JOIN wn_writing_number
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id 
JOIN leads_cstm
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1leads_ida = leads_cstm.id_c 
RIGHT JOIN leads
ON leads.id = leads_cstm.id_c 
JOIN wn_writing_number_cstm
ON wn_writing_number.id = wn_writing_number_cstm.id_c 
JOIN preps_contracted_reps_wn_writing_number_1_c
ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number_cstm.wn_writing_number_id_c 
JOIN preps_contracted_reps
ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
JOIN preps_contracted_reps AS upline_table
ON upline_table.id = wn_writing_number_cstm.id_c
WHERE
leads.deleted = '0'
GROUP BY
leads.id

我也尝试过这样的事情,但根据这篇文章:sql server - IN clause with multiple fields。您无法在IN语句中恢复多个列。这个让我回到正确的title_c。但是我还需要从上面选择的所有字段,因为它与下面的子查询有关。

SELECT
preps_contracted_reps.first_name AS rep_first_name,
preps_contracted_reps.last_name AS rep_last_name,
wn_writing_number_cstm.title_c
FROM
preps_contracted_reps_wn_writing_number_1_c
JOIN wn_writing_number_cstm
ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number_cstm.id_c 
JOIN preps_contracted_reps
ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
WHERE
preps_contracted_reps.id IN 
(SELECT
preps_contracted_reps.id
FROM
leads_wn_writing_number_1_c
JOIN wn_writing_number
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id 
JOIN leads_cstm
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1leads_ida = leads_cstm.id_c 
RIGHT JOIN leads
ON leads.id = leads_cstm.id_c 
JOIN wn_writing_number_cstm
ON wn_writing_number.id = wn_writing_number_cstm.id_c 
JOIN preps_contracted_reps_wn_writing_number_1_c
ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number_cstm.wn_writing_number_id_c 
JOIN preps_contracted_reps
ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
WHERE
leads.deleted = '0'
GROUP BY
leads.id)
GROUP BY
preps_contracted_reps.id

编辑:我已更新此问题以包含修订后的声明。当我尝试使用建议的别名加入它们时,第一个语句没有带回任何内容。

1 个答案:

答案 0 :(得分:0)

使用Pred的别名建议,我能够弄清楚我需要别名多于1个表。除了我想从中检索数据的表之外,我还需要再次为关系表添加别名。

SELECT
leads.id,
leads.date_entered,
leads.date_modified,
leads.first_name,
leads.last_name,
leads.primary_address_state,
leads.converted,
leads.`status`,
leads.lead_source,
leads_cstm.territory_c,
leads_cstm.contact_status_c,
leads_cstm.campaign_id_c,
leads_cstm.industry_c,
leads_cstm.contract_type_c,
leads_cstm.recruiting_link_sent_c,
preps_contracted_reps.first_name AS rep_first_name,
preps_contracted_reps.last_name AS rep_last_name,
upline_table.title_c
FROM
leads_wn_writing_number_1_c
JOIN wn_writing_number
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id 
JOIN leads_cstm
ON leads_wn_writing_number_1_c.leads_wn_writing_number_1leads_ida = leads_cstm.id_c 
RIGHT JOIN leads
ON leads.id = leads_cstm.id_c 
JOIN wn_writing_number_cstm
ON wn_writing_number.id = wn_writing_number_cstm.id_c 
JOIN preps_contracted_reps_wn_writing_number_1_c
ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number_cstm.wn_writing_number_id_c 
JOIN preps_contracted_reps
ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id 
JOIN preps_contracted_reps_wn_writing_number_1_c AS upline_relationship
ON preps_contracted_reps.id = upline_relationship.preps_cont9effed_reps_ida 
JOIN wn_writing_number_cstm AS upline_table
ON upline_relationship.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = upline_table.id_c
WHERE
leads.deleted = '0'
GROUP BY
leads.id