好的,所以我有包含以下数据的表格
表格过滤器
product_id filter_id
51 1
52 2
51 2
52 1
52 4
表格说明
group_id filter_id filter_name group_name
1 1 white COlour
1 2 black COlour
1 3 grey COlour
2 4 formal Style
2 5 casual Style
如果用户给定了一组过滤器 ID,我想通过将 filter_id 分组来过滤产品,考虑到他们的 group_id for ex。
select * from filter where filter_id in (1,2) AND filter_id in (4,5)
当我使用这个查询时,我得到空输出
为什么我在这里两次使用 IN 是因为 1,2 属于一个 filter_group 而 4,5 属于不同的 filter_group,可以在表中添加组和过滤器,因此我将根据用户选择使用 php 生成动态查询。
谁能帮我找到解决这个问题的方法,提前谢谢
最佳答案
根据您提供的示例,您需要执行与过滤和在这些子查询之间使用 inner join
的组一样多的子查询:
SELECT product_id
FROM
( SELECT product_id
FROM filter
WHERE filter_id IN (1,
2)) group1 // Here you select white or black products
INNER JOIN
( SELECT product_id
FROM filter
WHERE filter_id IN (4)) group2 // here products with Formal style
using(product_id) // Here both
关于mysql - 比较多个列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37894522/