简体   繁体   English

MySQL:涉及两个表和第三个关联表的复杂联接语句

[英]MySQL: Complex Join Statement involving two tables and a third correlation table

I have two tables that were built for two disparate systems. 我有两个表是为两个不同的系统构建的。 I have records in one table (called "leads") that represent customers, and records in another table (called "manager") that are the exact same customers but "manager" uses different fields 我在一个表(称为“线索”)中有代表客户的记录,而在另一表(称为“经理”)中有完全相同的客户记录,但“经理”使用不同的字段

(For example, "leads" contains an email address, and "manager" contains two fields for two different emails--either of which might be the email from "leads"). (例如,“潜在客户”包含一个电子邮件地址,“经理”包含两个不同电子邮件的两个字段,这两个字段都可能是“潜在客户”的电子邮件)。

So, I've created a correlation table that contains the lead_id and manager_id. 因此,我创建了一个包含lead_id和manager_id的关联表。 currently this correlation table is empty. 当前,此关联表为空。

I'm trying to query the "leads" table to give me records that match either "manager" email field with the single "leads" email field, while at the same time ignoring fields that have already been added to the "correlated" table. 我正在尝试查询“线索”表,以提供与“经理”电子邮件字段与单个“线索”电子邮件字段匹配的记录,同时忽略已添加到“相关”表中的字段。 (this way I can see how many leads that match have not yet been correlated.) Here's my current, invalid SQL attempt: (通过这种方式,我可以查看尚未匹配的潜在顾客数量。)这是我当前无效的SQL尝试:

SELECT leads.id, manager.id
  FROM leads, manager
  LEFT OUTER JOIN correlation ON correlation.lead_id = leads.id
  WHERE correlation.id IS NULL
  AND leads.project != "someproject"
  AND (manager.orig_email = leads.email OR manager.dest_email = leads.email)
  AND leads.created BETWEEN '1999-01-01 00:00:00' AND '2010-05-10 23:59:59'
  ORDER BY leads.created ASC;

I get the error: Unknown column 'leads.id' in 'on clause' 我收到错误:“ on子句”中的未知列“ leads.id”

Before you wonder: there are records in the "leads" table where leads.project != "someproject" and leads.created falls between those dates. 奇怪的是:“ leads”表中有记录,其中Leads.project!=“ someproject”和Leads.created介于这些日期之间。 I've included those additional parameters for completeness. 为了完整起见,我包括了这些附加参数。

You have the tables listed the wrong way round so the LEFT OUTER JOIN is joining manager to correlation instead of leads to correlation as you intended. 您以错误的方式列出了表格,因此LEFT OUTER JOIN正在将manager加入correlation而不是按预期leads correlation Try swapping them round: 尝试将它们四处交换:

SELECT leads.id, manager.id
  FROM manager, leads
  LEFT OUTER JOIN correlation ON correlation.lead_id = leads.id
  WHERE correlation.id IS NULL
  AND leads.project != "someproject"
  AND (manager.orig_email = leads.email OR manager.dest_email = leads.email)
  AND leads.created BETWEEN '1999-01-01 00:00:00' AND '2010-05-10 23:59:59'
  ORDER BY leads.created ASC;

In general I'd recommend not mixing old and new style joins to avoid exactly this type of error. 总的来说,我建议不要混合使用新旧样式的连接,以免发生此类错误。 I'd prefer to see something like this: 我希望看到这样的东西:

SELECT leads.id, manager.id
  FROM leads
  LEFT OUTER JOIN correlation ON correlation.lead_id = leads.id
  JOIN manager ON manager.orig_email = leads.email OR manager.dest_email = leads.email
  WHERE correlation.id IS NULL
  AND leads.project != "someproject"
  AND leads.created BETWEEN '1999-01-01 00:00:00' AND '2010-05-10 23:59:59'
  ORDER BY leads.created ASC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM