当前位置: 代码迷 >> Sql Server >> 该怎么查询
  详细解决方案

该怎么查询

热度:20   发布时间:2016-04-27 14:13:39.0
该如何查询
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 twhere (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 @Tselect 1111,1 union allselect 1111,2 union allselect 2222,1 union allselect 2222,3select product_id from (select * from @T where filter_id=1union allselect * 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 这条数据

该语句应该怎么写
  相关解决方案