gpt4 book ai didi

mysql - 使用 mysql 查找具有 2 个关联(一起)的项目

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:01 25 4
gpt4 key购买 nike

我有包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com