我有包含 3 个表的数据库:
product (id, name, price, ...)
product_option (id, product_id, option_id)
option (id, value)
产品有多种选择
例如,我需要找到具有选项 A 和选项 B 的产品,而不是仅具有选项 A 的产品。
我正在使用这样的想法:
select * from product
left join product_option on product_option.product_id = product_id
where product_option.option_id in (1,2,3)
and product_option.option_id in (4)
group by product.id
当然结果总是空的。 我需要带有 option_id (1 or 2 or 3) and 4
的产品我该怎么做?
最佳答案
因为你想要两个不同的匹配,你需要加入 product_option twise
select * from product
left join product_option AS po1 on product_option.product_id = product_id
left join product_option AS po2 on product_option.product_id = product_id
where po1.option_id in (1,2,3)
and po2.option_id in (4)
group by product.id
关于mysql - 使用 mysql 查找具有 2 个关联(一起)的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7012983/