[英]Select on many to many relation
我有兩個實體Product(int id ID)
和Attribute(int id ID)
。 每個產品都可以有多個具有自定義值的屬性,因此我有一個ProductAttribute(int product_id, int attribute_id, int value)
。
我想搜索具有值為x的屬性1和具有值y和...的屬性2的產品。
那可能嗎? 如何? 或者我應該更改關系圖?
如果重要的話,我正在使用MySQL 5.7。
您可以使用聚合並having
:
select product_id
from ProductAttribute
where (attribute_id, value) in ( (1, 2), (2, 3), (3, 4))
group by product_id
having count(*) = 3;
“3”是要比較的列表中的屬性數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.