Question

I would like to select users who have not posted a job.

I have tried this:

SELECT 
    e.id, e.companyName, ee.emailAddress, ee.firstName, ee.surname    
FROM 
    employer as e, country as country, employerUser as ee , countryRegion as re, job as j 
WHERE 
    country.iso2=e.countryISO2FK AND ee.employerIDFK=e.id AND ee.originalUser=1 AND country.iso2='GB' AND (e.regionCodeFK ='GTL' AND e.regionCodeFK = re.code OR e.regionCodeFK ='' OR e.city ='' ) AND e.status=1 AND e.isActive=1 AND e.id=j.employerIDFK AND j.employerIDFK IS NULL

Does anyone know what I'm doing wrong?

Was it helpful?

Solution

You are using a CROSS JOIN with a WHERE filter, which won't let you find items that don't exist. What you want to use is a LEFT JOIN on the 'job' table like this:

SELECT e.id, e.companyName, ee.emailAddress, ee.firstName, ee.surname 
FROM employer as e
  JOIN country ON country.iso2=e.countryISO2FK
  JOIN employerUser as ee ON ee.employerIDFK=e.id
  LEFT JOIN job as j ON e.id=j.employerIDFK 
WHERE ee.originalUser=1 
  AND country.iso2='GB' 
  AND (e.regionCodeFK ='GTL' AND e.regionCodeFK = re.code OR e.regionCodeFK ='' OR e.city ='' ) 
  AND e.status=1 AND e.isActive=1  
  AND j.employerIDFK IS NULL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top