请教一个算法问题!

lsy110 2004-08-02 11:33:53
在数据库中有两个TABLE:table1 and table2。通过5个KEY,我要查找两个TABLE中相同的记录。具体要实现:

如果table1中的记录没有在table2中找到(5个Key都相同)。insert 当前记录 into table3

如果table1中的记录在table2中找到相同的5个KEY。把table2的当前数据insert into table3

我现在是通过两个循环来做。。数据量很大的时候太慢太慢了。各位大侠有什么好的算法建议?又或者可以有其他方法解决上面这个问题??

非常感谢!!!

...全文
85 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsy110 2004-08-03
  • 打赏
  • 举报
回复
谢谢上面各位。

有一个问题:table1 和 table2不是来自一个DATABASE,table1是一个EXCEL文件,table2是一个DATABASE TABLE。

Table3是一个DATABASE TABLE。

不过都可以通过创建临时TABLE来实现了。。。我试试看。。谢谢!

friendliu 2004-08-03
  • 打赏
  • 举报
回复
不用循环语句
insert table3 select * from table2 A where not exists(select 1 from from table2 Bwhere A.key1=B.key1 and A.key2=B.key2 and A.key3=B.key3 and A.key4=B.key4 and A.key5=B.key5)

insert table3 select * from table1 A where not exists(select 1 from from table1 Bwhere A.key1=B.key1 and A.key2=B.key2 and A.key3=B.key3 and A.key4=B.key4 and A.key5=B.key5)
lsy110 2004-08-03
  • 打赏
  • 举报
回复
谢谢。。。果然快了很多。。。谢谢各位。
j9988 2004-08-02
  • 打赏
  • 举报
回复
一句:
insert table3(....)
//如果table1中的记录在table2中找到相同的。把table2的当前数据insert into table3
select * from table1 A where not exists(select 1 from table2 where key1=A.key1 and ....key5=A.key5)
union all
//如果table1中的记录没有在table2中找到(5个Key都相同)。insert 当前记录 into table3
select * from table2 A where exists(select 1 from table1 where key1=A.key1 and ....key5=A.key5)
lsy110 2004-08-02
  • 打赏
  • 举报
回复
大家帮忙啊。。。

34,575

社区成员

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

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