我有两张表格如下
user_job_applied
company_viewed_user
我想在user_job_applied表中获取不在company_viewed_user表中的相关job_id的行。 我为它编写了以下查询,但没有抓住
SELECT
`user_job_applied`.user_id
FROM
`user_job_applied`
LEFT JOIN
`company_viewed_user`
ON
`company_viewed_user`.user_id ON `company_viewed_user`.user_id = `user_job_applied`.user_id
WHERE
`company_viewed_user`.user_id IS NULL AND
`company_viewed_user`.emp_id IN (SELECT user_id
FROM company_user
WHERE company_id='1')
AND
`user_job_applied`.job_id = '1';
答案 0 :(得分:0)
您可以使用以下查询
SELECT uja.user_id
FROM `user_job_applied` AS uja
LEFT JOIN `company_viewed_user` AS cvu ON cvu.user_id = uja.user_id
WHERE cvu.user_id IS NULL;
如果您想添加任何其他过滤器,那么您可以加入自己的过滤器或让我知道。
添加新条件
SELECT uja.user_id
FROM `user_job_applied` AS uja
JOIN company_user AS cu ON cu.user_id=uja.emp_id
LEFT JOIN `company_viewed_user` AS cvu ON cvu.user_id = uja.user_id
WHERE uja.job_id=1 AND cu.company_id=1 AND cvu.user_id IS NULL;
注意:如果job_id和company_id不是Int类型,则用引号括起来。
答案 1 :(得分:0)
希望将emp_id条件移动到where子句
中的左连接语句SELECT
`user_job_applied`.user_id
FROM
`user_job_applied`
LEFT JOIN `company_viewed_user` ON `company_viewed_user`.user_id = `user_job_applied`.user_id AND
`company_viewed_user`.emp_id IN (SELECT user_id FROM company_user WHERE company_id='1')
WHERE
`company_viewed_user`.user_id IS NULL AND
`user_job_applied`.job_id = '1';