sql查询语句小问题..查询在结果1中查询出结果2中不存在的记录!!

q85958341 2010-01-14 11:05:43
如何连接结果1和2,我想查询在结果1中查询出结果2中不存在的记录!!

如果用 not exists ,条件又要怎么写!

--结果1
select * from consume_main_m where stationno=-10


--结果2
select a.cardid,a.consumeorder,a.curtime,a.stationno,a.consume,a.spare,a.computer_id,a.manage_id,a.manage_name from consume_main_m a,
(
select * from consume_main_m
where
convert(varchar(10),curtime,120)>='2009-08-25'
) b
where a.cardid=b.cardid and a.consumeorder=b.consumeorder and
a.consume=b.consume and a.spare=b.spare and a.stationno!=b.stationno

...全文
172 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qizhengsheng 2010-01-14
  • 打赏
  • 举报
回复
xiao F
那个是sql2005的
q85958341 2010-01-14
  • 打赏
  • 举报
回复
执行了N久都没出结果
q85958341 2010-01-14
  • 打赏
  • 举报
回复
数据库2000
select * from consume_main_m t where stationno=-10 
and not exists
(select 1 from consume_main_m a,
(select * from consume_main_m where convert(varchar(10),curtime,120)>='2009-08-25') b
where a.cardid=b.cardid and a.consumeorder=b.consumeorder and a.consume=b.consume
and a.spare=b.spare and a.stationno!=b.stationno
and a.cardid=t.cardid)
--小F-- 2010-01-14
  • 打赏
  • 举报
回复
;with f1 as
(
select * from consume_main_m where stationno=-10
)
f2 as
(
select a.cardid,a.consumeorder,a.curtime,a.stationno,a.consume,a.spare,a.computer_id,a.manage_id,a.manage_name from consume_main_m a,
(
select * from consume_main_m
where
convert(varchar(10),curtime,120)>='2009-08-25'
) b
where a.cardid=b.cardid and a.consumeorder=b.consumeorder and
a.consume=b.consume and a.spare=b.spare and a.stationno!=b.stationno
)
select
*
from
f2
where
not exists(select 1 from f1 where stationno=f2.stationno)
pt1314917 2010-01-14
  • 打赏
  • 举报
回复

--try:

select * from consume_main_m t where stationno=-10
and not exists
(select 1 from consume_main_m a,
(select * from consume_main_m where convert(varchar(10),curtime,120)>='2009-08-25') b
where a.cardid=b.cardid and a.consumeorder=b.consumeorder and a.consume=b.consume
and a.spare=b.spare and a.stationno!=b.stationno and cardid=t.cardid)

34,837

社区成员

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

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