我想写一些查询,它给我带有特定键值的产品
tbl_product
product_id field_id value
1 1 100
1 2 200
我想找到所有产品(field_id = 1和value = 100)和(field_id = 2且值= 200)
答案 0 :(得分:0)
您可以使用where
,group by
和having
:
select product_id
from t
where (field_id = 1 and value = 100) or (field_id = 2 and value = 200 )
group by product_id
having count(*) = 2;
如果允许重复,则需要count(distinct field_id) = 2
。
答案 1 :(得分:0)
我想你似乎想要:
select t.*
from table t
where exists (select 1
from table
where product_id = t.product_id and (field_id = 1 and value = 100)
) and
exists (select 1
from table
where product_id = t.product_id and (field_id = 2 and value = 200)
);