MySQL-1toN,从table1中选择table2.attribute=1和table2.attribute=2的项目

MySQL-1toN,从table1中选择table2.attribute=1和table2.attribute=2的项目,mysql,subquery,Mysql,Subquery,我使用四个表,我将它们称为产品、属性、类别、属性和产品属性。products中的每个产品都有许多属性,product_中的每个产品都有以下属性: [product_has_attributes] : product1 attribute_id=1 (Brands->Sony) attribute_id=4 (Screen size -> 20") attribute_id=7 (Colors -> Black) attribute_id=8 (Colors -> Whit

我使用四个表,我将它们称为产品、属性、类别、属性和产品属性。products中的每个产品都有许多属性,product_中的每个产品都有以下属性:

[product_has_attributes] : product1
attribute_id=1 (Brands->Sony)
attribute_id=4 (Screen size -> 20")
attribute_id=7 (Colors -> Black)
attribute_id=8 (Colors -> White)
products和product_has_属性在products上保持连接。id=product_has_属性。product_id

一个简单的选择正确地返回每个产品与其属性相同的次数

现在,我想选择所有具有以下功能的产品:

product_has_attributes.attribute_id=1 AND 
product_has_attributes.attribute_id=4 AND 
(product_has_attributes.attribute_id=7 OR 
product_has_attributes.attribute_id=8)
但是,正如预期的那样,产品\u具有\u属性。属性\u id不能同时为1和4以及7或8。。。因此,不会返回任何记录

如何构建SQL,使其按照我描述的逻辑返回记录

谢谢,,
乔治你可以用这样的东西。效率不高

 select * from products 
 where product_id in (
     select product_id from product_has_attributes where attribute_id = 1
 )
 and product_id in (
     select product_id from product_has_attributes where attribute_id = 4
 )
 and product_id in (
     select product_id from product_has_attributes where attribute_id in (7, 8)
 )

谢谢,但出于某种原因,它只返回第一个匹配的行。我是用您的输入计算出来的。谢谢克里斯基!