一个项table: items使用一个连接表item_taxonomies (item_id, taxonomy_id)有许多分类器table: taxonomies。
使用分类法组搜索项。
示例:
taxo_group_1 = [1, 2, 3]
taxo_group_2 = [4, 5]items应该会发现所有taxonomies都包含在这两个数组中:
如果我有这些元素:
item_1 id=1
taxo_1 id=11
taxo_2 id=12
taxo_3 id=13
item_2 id=2
taxo_3 id=13
taxo_4 id=14使用[11, 12]和[13]进行搜索将返回item_1而不是item_2,因为item_1有分类法in [11, 12] AND in [13]。
item_2不会被返回,因为它在[11, 12]中没有分类法
迄今为止:
"taxonomies"."id" IN (11, 12, 13) AND "taxonomies"."id" IN (13)当然不行了。
发布于 2018-03-07 14:16:07
您可以使用以下查询获得所需的输出:
select item from (
select item, count(distinct taxonomy_id) as count from items
join taxonomies on items.item_id = taxonomies.item_id
where taxonomies.taxonomy_id in (11,12,13)
group by item
) as T where count = 3发布于 2018-03-07 13:53:28
我对此有一点不同。他/她希望找到所有有11,12和13作为分类id的项目。
select item from joinedTable
where taxonomy_id in (11,12,13)
group by item
having count(distinct taxonomy_id) = 3但实际上,我对这个请求有点困惑。
发布于 2018-03-07 13:09:05
如果我完全理解您的问题,您希望得到一个在条目中获得所有id的组,在sql中,它将是:
SELECT [...] WHERE taxonomies.id = 11
AND taxonomies.id = 12
AND taxonomies.id = 13https://stackoverflow.com/questions/49152638
复制相似问题