有三张表,表Product有字段ID,表Attribute有字段ID,表Product_Attribute_Mapping有字段ProductId、AttributeId,现在我要根据多个AttributeId查出Product,就是查询具有所有这些属性的产品,怎么搞?

解决方案 »

  1.   

    select * 
    from Product as a 
    where not exists(select 1 from Attribute as b where not exists(select 1 from Product_Attribute_Mapping where ProductId=a.ProductId and AttributeId=b.AttributeId))
      

  2.   

    select a.* from product a inner join product_Attribute_Mapping b on a.id=b.productid
    inner join attribute c on b.attributeid=c.id
    where c.属性 in(这里放属性列表)
      

  3.   

    改改字段名
    select * from Product as a where not exists(select 1 from Attribute as b where not exists(select 1 from Product_Attribute_Mapping where ProductId=a.ID and AttributeId=b.ID))
      

  4.   

    select
      a.id
    from
      Product a,Attribute b,Product_Attribute_Mapping c
    where
      a.id=c.ProductId
    and
      b.id=c.AttributeId
    group by
      a.id
    having count(b.id)=(select count(AttributeId) from Product_Attribute_Mapping)