我有两张桌子
Table "public.tags_to_entities"
Column | Type | Modifiers
--------+---------+-----------
tid | integer | not null
eid | integer | not null
Table "public.tag_tree"
Column | Type | Modifiers
--------+---------+-----------
tid | integer | not null
pid | integer | not null
level | integer |
tag_tree
包含标签之间的所有关系,意思SELECT pid FROM tag_tree WHERE tid = ?
是返回该标签的父母。该level
列存在或ORDER BY
仅存在。
我想返回所有eid
标签子集中至少有一个标签的列表。使用以下查询执行一个子集
SELECT DISTINCT eid
FROM tags_to_entities
WHERE
tags_to_entities.tid = 1 OR
tags_to_entities.tid IN (SELECT tag_tree.tid FROM tag_tree WHERE tag_tree.pid = 1));
这将返回eid
标签 1 或其子标签之一中存在的所有内容。如果我想返回eid
至少一个与1
和 2
相关的标签中存在的所有内容。到目前为止我失败的方法是
SELECT DISTINCT eid
FROM tags_to_entities
WHERE
(
tags_to_entities.tid = 1 OR
tags_to_entities.tid IN (SELECT tag_tree.tid FROM tag_tree WHERE tag_tree.pid = 1)) AND
(
tags_to_entities.tid = 2 OR
tags_to_entities.tid IN (SELECT tag_tree.tid FROM tag_tree WHERE tag_tree.pid = 2));
这不起作用,因为 tid 不能同时为 1 和 2。我该如何解决这个问题?(eid 稍后将与条目表连接)