product_id property_id
1 2
1 5
2 2
3 5
我有一个如上的映射表。如果 product_id in (2,5)
,我只想获得 id =1
的产品。即,如果表同时包含 2,5
我想获取数据,而不是如果它包含 property_id
只有 2 或 5
select group_concat(distinct product_id) product_ids from table where property_id in (2,5)
更新: property_id in 可以是 property_id in(2,5,....)。我从表单中得到的输出为 2,5,.... 等等。它不仅仅是针对单个案例。如果 property_id 中的条件与整个系列匹配,我只想要输出。
最佳答案
这是怎么做到的
select
product_id from
table_name
where property_id in (2,5)
group by product_id
having count(*) = 2
您只需将 having count(*) = 2
更改为 IN()
中的项目数,现在是 2,如果您正在查看 3 property id 那么它将是 3 等等。
关于php - 如果条件在多行中匹配,则从映射表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949893/