34,590
社区成员
发帖
与我相关
我的任务
分享
select *from
(select * from #t where 类别='A')a
left join (select * from #t where 类别='B')b on a.商品名=b.商品名
where a.数量>0 and b.数量<1 and a.价格=b.价格
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (类别 varchar(11),商品名 varchar(11),数量 int,价格 int)
insert into #T
select 'A','AA',1,10 union all
select 'A','BB',1,10 union all
select 'A','CC',1,10 union all
select 'B','AA',1,10 union all
select 'B','BB',1,10 union all
select 'B','CC',2,20
select * from #T as t where 类别='A' and not exists (select 1 from #T where 类别='B' and 商品名=t.商品名 and 价格=t.价格)
/*
类别 商品名 数量 价格
----------- ----------- ----------- -----------
A CC 1 10
*/
改一下
select * from
(select * from [Table1] where 类别='A')aa
Left Join
(select * from [Table1] where 类别='B')bb
on aa.商品名=bb.商品名 and aa.价格=bb.价格 where bb.商品名 is null
select * from
(select * from [Table1] where 类别='A')aa
Left Join
(select * from [Table1] where 类别='A')bb
on aa.商品名=bb.商品名 and aa.价格=bb.价格 where bb.商品名 is null