我目前使用的软件中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.
答案 0 :(得分:5)
select object_id
from tags_link
where tag_id in (31,35)
group by object_id
having count(distinct tag_id) = 2
答案 1 :(得分:4)
select object_id from tags_link where tag_id = 31
intersect
select object_id from tags_link where tag_id = 35