日期:2014-05-18 浏览次数:20793 次
select * from ProductSite a where siteid = 1 and not exists (select 1 from ProductSite where siteid <> 1 and productid = a.productid )
------解决方案--------------------
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test](
[id] int,
[productId] int,
[siteId] int
)
insert [test]
select 1,1,1 union all
select 2,2,1 union all
select 3,3,1 union all
select 4,1,2 union all
select 5,2,2
select * from test a
where not exists(select 1 from test b
where a.siteId<>b.siteId and a.productId=b.productId)
/*
id productId siteId
---------------------------------
3 3 1
*/
==你是要这个结果吗?
------解决方案--------------------
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test](
[id] int,
[productId] int,
[siteId] int
)
insert [test]
select 1,1,1 union all
select 2,2,1 union all
select 3,3,1 union all
select 4,1,2 union all
select 5,2,2
--如果需要以siteId=1为标准
select * from test a
where not exists(select 1 from test b
where a.productId=b.productId and a.siteId<>b.siteId)
and a.siteId=1
/*
id productId siteId
---------------------------------
3 3 1
*/