gpt4 book ai didi

SQL:相当于逻辑 AND 的 IN 语句

转载 作者:行者123 更新时间:2023-12-04 14:45:49 26 4
gpt4 key购买 nike

我目前使用一个软件,SQL表中的对象和标签之间存在如下关系:

+-----------+--------+
| object_id | tag_id |
+-----------+--------+
| 226 | 31 |
| 226 | 35 |
| 313 | 31 |
| 312 | 35 |
+-----------+--------+

使用逻辑 OR 获取标签 ID 列表的所有对象 ID 可以使用以下请求:

select distinct object_id from tags_link where tag_id in (31, 35);

如何才能获得具有逻辑AND的标签ID列表的所有对象ID?

对于上表,对标签 id 31 和 35 的请求应该只返回 object_id 226。

最佳答案

select object_id
from tags_link
where tag_id in (31,35)
group by object_id
having count(distinct tag_id) = 2

关于SQL:相当于逻辑 AND 的 IN 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13815481/

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