gpt4 book ai didi

OR 运算符的 SQL 语句 WHERE 子句问题

转载 作者:行者123 更新时间:2023-12-04 14:37:50 25 4
gpt4 key购买 nike

我对这个 SQL 查询有疑问:

SELECT user_id, scrap_id, scrap_text, profile_id,  add_date
FROM scraps
WHERE profile_id=52 AND user_id=68 OR user_id
IN(
SELECT user_id
FROM profilecontact
WHERE owner_id=68 AND profile_id=52
);

此查询的问题在于 OR 运算符出现的这一部分。我需要 SQL 按以下方式思考:

profile_id=52 <- This one should always be followed
user_id=68 OR user_id IN... <- The result may have user_id 68 OR a user_id that the sub query finds.

SQL现在做的是完全忽略这部分:

profile_id=52 AND user_id=68

如何按我需要的方式使用 OR 和 AND 运算符?

最佳答案

您只需要用括号组织您的标准。

试试这个:

SELECT user_id, scrap_id, scrap_text, profile_id,  add_date
FROM scraps
WHERE profile_id=52 AND (user_id=68 OR user_id
IN(
SELECT user_id
FROM profilecontact
WHERE owner_id=68 AND profile_id=52
));

这会将 user_id 条件视为单个元素,将 profile_id 视为另一个元素,AND将它们组合在一起。

关于OR 运算符的 SQL 语句 WHERE 子句问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23546972/

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