Question

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. 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:

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'

Before you wonder: there are records in the "leads" table where leads.project != "someproject" and leads.created falls between those dates. I've included those additional parameters for completeness.

Was it helpful?

Solution

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. 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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top