sqlserver 查询语句问题

wangyanboq 2008-05-28 10:05:33
有数据库表table1如下
类别 商品名 数量 价格
A AA 1 10
A BB 1 10
A CC 1 10
B AA 1 10
B BB 1 10
B CC 2 20

要查询在A类里面有,而B类里面没有,且售价一样的商品
sql语句怎么写
...全文
57 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
撩天槌 2008-05-28
  • 打赏
  • 举报
回复
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.价格

是这意思吗
-狙击手- 2008-05-28
  • 打赏
  • 举报
回复
如果是批商品 ,如果商品在B类没有何价格一样?
-狙击手- 2008-05-28
  • 打赏
  • 举报
回复
要查询在A类里面有,而B类里面没有


---

你指商品 没有?
Limpire 2008-05-28
  • 打赏
  • 举报
回复
--> 测试数据: #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
*/
chuifengde 2008-05-28
  • 打赏
  • 举报
回复
改一下
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
chuifengde 2008-05-28
  • 打赏
  • 举报
回复
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

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧