日期:2014-05-18  浏览次数:20518 次

该如何查询
product_id filter_id
1111 1
1111 2
2222 1
2222 3

现有两个条件 filter_id=1 和 filter_id=2

我只想查询出 product_id=1111 这条数据

该语句应该怎么写

------解决方案--------------------
SQL code
select *
from tb t
where 
(select count(distinct filter_id) from tb where product_id=t.product_id and filter_id in(1,2))=2

------解决方案--------------------
SQL code

declare @T table (product_id int,filter_id int)
insert into @T
select 1111,1 union all
select 1111,2 union all
select 2222,1 union all
select 2222,3

select product_id from 
(
select * from @T where filter_id=1
union all
select * from @T where filter_id=2
) a group by product_id having(count(1)=2)
/*
product_id
-----------
1111
*/

------解决方案--------------------
探讨
product_id filter_id
1111 1
1111 2
2222 1
2222 3

现有两个条件 filter_id=1 和 filter_id=2

我只想查询出 product_id=1111 这条数据

该语句应该怎么写