我有两个表:jobs和job_industries(加入jobs.id=job_industries.job_id
)。
我想找到jobs.title like '%Finance%'
而没有job_industries.industry=1
的所有工作。
但是,在运行下面的查询时,我会获得与标题条件匹配的每个作业的列表,并且至少有一个行业选项不是1
。
select jobs.id, title, industry
from `job_industries`
left join jobs on jobs.id=`job_industries`.job_id
where is_live=1 and jobs.`is_closed`=0 and 1 in (select industry from job_industries where job_id in )
e.g。如果作业有两个job_industry关系(industry=1
和industry=2
),则此作业将与查询匹配。我宁愿找到那些没有的工作。
答案 0 :(得分:4)
SELECT j.id, j.title, ji.industry
FROM jobs j
INNER JOIN job_industries ji
ON j.id = ji.job_id
AND ji.industry <> 1
WHERE j.title LIKE '%Finance%'
AND j.is_live = 1
AND j.is_closed = 0
AND NOT EXISTS (SELECT 1
FROM job_industries ji2
WHERE ji2.job_id = j.id
AND ji2.industry = 1);
或者,你可以这样做:
SELECT j.id, j.title, ji.industry
FROM jobs j
INNER JOIN job_industries ji
ON j.id = ji.job_id
AND ji.industry <> 1
LEFT JOIN job_industries ji2
ON j.id = ji2.job_id
AND ji2.industry = 1
WHERE j.title LIKE '%Finance%'
AND j.is_live = 1
AND j.is_closed = 0
AND ji2.job_id IS NULL;
答案 1 :(得分:0)
select jobs.id, title, industry
from `job_industries`
left join jobs on jobs.id=`job_industries`.job_id AND job_industries.industry=1
where is_live=1 and jobs.`is_closed`=0 and job_industries.industry IS NULL