gpt4 book ai didi

MySQL 使用 NOT IN 作为临时表

转载 作者:行者123 更新时间:2023-11-30 23:23:31 25 4
gpt4 key购买 nike

我希望能够在类别表中找到条件为 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

最佳答案

你不尝试使用 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

关于MySQL 使用 NOT IN 作为临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14451007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com