gpt4 book ai didi

sql - 与 UNION SQL 查询相反

转载 作者:行者123 更新时间:2023-11-29 14:20:44 26 4
gpt4 key购买 nike

我有 2 个表:

interests (storing the interest ID and name)
person_interests(storing the person_id and interest_id)

我如何选择特定人未选择的所有兴趣?

我已经尝试了以下 SQL 查询,但仍然没有得到想要的结果

SELECT * 
FROM interests LEFT JOIN person_interests
ON interests.id=person_interests.person_id
WHERE person_interests.id IS NULL
AND person_id=66;

最佳答案

使用NOT EXISTS

SELECT *
FROM interests
WHERE NOT EXISTS (
SELECT person_interests.interest_id
FROM person_interests
WHERE person_id = 66
AND interests.id = person_interests.interest_id
)

关于sql - 与 UNION SQL 查询相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28287879/

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