我希望能够在Category表中找到条件为visible = 0
但不在下面的查询结果中的行:
SELECT c.id as cat_id, j.id
FROM category c
LEFT JOIN portfolio_job_category pjc
ON pjc.category_id = c.id
LEFT JOIN job j
ON j.id = pjc.job_id
WHERE c.visible = 0
AND j.end_date > CURRENT_DATE
GROUP BY c.id
有没有办法使用NOT IN
来找到它?
我在下面试过,但这会永远循环并崩溃mysqld
SELECT c.id
FROM category c
WHERE c.id NOT IN
(SELECT c.id as cat_id, j.id
FROM category c
LEFT JOIN portfolio_job_category pjc
ON pjc.category_id = c.id
LEFT JOIN job j
ON j.id = pjc.job_id
WHERE c.visible = 0
AND j.end_date > CURRENT_DATE
GROUP BY c.id)
AND visible = 0
答案 0 :(得分:0)
您尝试使用LEFT JOIN
SELECT a.ID
FROM category a
LEFT JOIN
(
SELECT c.id as cat_id
FROM category c
LEFT JOIN portfolio_job_category pjc
ON pjc.category_id = c.id
LEFT JOIN job j
ON j.id = pjc.job_id
WHERE c.visible = 0 AND
j.end_date > CURRENT_DATE
) b ON a.ID = b.ID
WHERE b.ID IS NULL AND
a.visible = 0