首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

该怎么查询

2012-04-17 
该如何查询product_idfilter_id11111111122222122223现有两个条件 filter_id1 和 filter_id2我只想查询

该如何查询
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 这条数据

该语句应该怎么写

热点排行