mysql选择尚未在网站上发布的用户

时间:2012-12-31 23:25:51

标签: mysql join mysql5

我想选择尚未发布职位的用户。

我试过这个:

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

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

您正在使用CROSS JOINWHERE过滤器,这样您就无法找到不存在的项目。您要使用的是'job'表中的LEFT JOIN,如下所示:

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