求一SQL,各位救命呀.解决就给分.决不含糊.

liaorui 2004-09-08 11:19:51
两个表 temp 和 temp1
temp表结构:
aa bb
1111 1111
1111 2222
1111 3333
1111 4444
2222 1111
2222 2222
2222 3333
2222 4444
3333 1111
3333 2222
3333 3333
3333 4444
create table temp(aa nvarchar(4),bb nvarchar(4))
insert into temp values('1111','1111')
insert into temp values('1111','2222')
insert into temp values('1111','3333')
insert into temp values('1111','4444')
insert into temp values('2222','1111')
insert into temp values('2222','2222')
insert into temp values('2222','3333')
insert into temp values('2222','4444')
insert into temp values('3333','1111')
insert into temp values('3333','2222')
insert into temp values('3333','3333')
insert into temp values('3333','4444')

temp1表结构
cc bb
1111 2222
1111 3333
1111 4444
2222 1111
2222 3333
2222 4444
3333 1111
3333 2222
3333 4444
create table temp1(cc nvarchar(4),dd nvarchar(4))
insert into temp1 values('1111','2222')
insert into temp1 values('1111','3333')
insert into temp1 values('1111','4444')
insert into temp1 values('2222','1111')
insert into temp1 values('2222','3333')
insert into temp1 values('2222','4444')
insert into temp1 values('3333','1111')
insert into temp1 values('3333','2222')
insert into temp1 values('3333','4444')

返回结果:
1111 1111
2222 2222
3333 3333
也就是说通过SQL返回Temp表里有但Temp1表里没有的记录.
如果不清楚题目的可以加我
QQ:29239212
MSN:jeffery_liao@hotmail.com
谢谢各位了!!
...全文
212 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowolf_ren 2004-09-09
  • 打赏
  • 举报
回复
看了一下,好象有人都写过这个了
snowolf_ren 2004-09-09
  • 打赏
  • 举报
回复
select * from temp where aa+bb not in (select cc+bb from temp1)
Andy__Huang 2004-09-09
  • 打赏
  • 举报
回复
上面得到的結果是:
1111 1111
2222 2222
3333 3333
-------------------------------------------------

但是還有點問題,如果某一記錄也在temp1中唯一存在,記錄也被選出來。所以要改一下
select * from
(select distinct * from temp
union all
select * from(
select * from temp
union all select * from temp1
)a group by aa,bb having count(*)=1
)b group by aa,bb having count(*)>1

真正你要的答案是這個,
結果:
1111 1111
2222 2222
3333 3333


Andy__Huang 2004-09-09
  • 打赏
  • 举报
回复
select * from(
select * from temp
union all
select * from temp1
)a group by aa,bb having count(*)=1
haohaohappy 2004-09-09
  • 打赏
  • 举报
回复
select * from temp where not exists(select * from temp1 where temp1.cc=temp.aa and temp1.bb=temp.bb and temp1.cc=temp.bb and temp1.bb=temp.aa)
RUKYO 2004-09-09
  • 打赏
  • 举报
回复
select * from temp where (temp.aa + temp.bb) not in (select (d.aa+d.bb)as d_ab from(select temp.aa,temp.bb from temp inner join temp1 on temp.aa = temp1.cc and temp.bb = temp1.bb)as d)
caojinrong 2004-09-09
  • 打赏
  • 举报
回复
怎么这么多人散分?我也要一份。
hsanzhang 2004-09-09
  • 打赏
  • 举报
回复
up
liaorui 2004-09-08
  • 打赏
  • 举报
回复
我的意思如果这道题在ORACLE里,但不用连接符的这种处理方法,那么在ORACLE里应该怎么处理.
helanshan 2004-09-08
  • 打赏
  • 举报
回复
up
阿泰 2004-09-08
  • 打赏
  • 举报
回复
如果字段多了的话可能就要考虑是不是设计上的问题了
在Oracle里 + 可以做连接符号吗??俺平时是这么用
select * from temp where aa||bb not in (select cc||bb from temp1)

楼主的意思好象只是a<>c,b<>d
那么用应该就可以满足了啊
select temp.* From temp,temp1 where temp1.cc<>temp.aa and temp1.dd<>temp.bb



liaorui 2004-09-08
  • 打赏
  • 举报
回复
楼上那条SQL没问题.可以...

但如果在ORACLE里就有问题了..
请楼下的回答在ORACLE里的处理.谢谢
我的处理方法:
select * from temp where aa+bb not in (select cc+bb from temp1)
但如果字段多了,就有问题了.
online 2004-09-08
  • 打赏
  • 举报
回复
select * from temp where not exists(select * from temp1 where temp1.cc=temp.aa and temp1.dd=temp.bb)

改了一下字段
bladeinside 2004-09-08
  • 打赏
  • 举报
回复
select * from temp where not exist (select * from templ)
New小青龙 2004-09-08
  • 打赏
  • 举报
回复
select * from temp where aa+bb not in (select cc+bb from temp1)
liaorui 2004-09-08
  • 打赏
  • 举报
回复
MD..搞定了...

就当散分好了.
wd_318 2004-09-08
  • 打赏
  • 举报
回复
try:

select * from temp where not exists(select * from temp1 where temp1.cc=temp.aa and temp1.bb=temp.bb)

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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