SQL比Delphi难多了!!! (200分)

死水

Unregistered / Unconfirmed
GUEST, unregistred user!
oldtable
name address
张三 北京市
李四 上海市
赵五 武汉市

newtable
name address
张三 北京市王府井
李四 上海市南京路
孙六 广州市

我希望用newtable里面的数据来更新oldtable,

更新后的oldtable为
name address
张三 北京市王府井
李四 上海市南京路
赵五 武汉市
孙六 广州市

请问这个sql怎么写?能不能用一条sql实现?
太感谢您了!!!
 
以缩略图方式显示某一目录下所有图片文件
 
update oldtable o
set o.address=(select address from newtable n where n.name=o.name)

最好是这两个表的name字段都有索引
 
不行,只能用两句语句实现;
1.update oldtable有的数据
update oldtable o
set o.address=(select n.address from newtable n where n.name=o.name)
2.insert oldtable没有的数据
insert into oldtable
(select n.* from newtable n,oldtable o
where n.name = o.name(+)
and o.name is null)
 
我不但要更新数据,还可能需要添加数据,请问可以只用一条sql语句吗?

如果只用一条sql实现不了,那请问怎样做比较高效?
因为我的newtable有好多个,我想把newtable,newtable1,newtable2...都要弄到oldtable中去

请教教俺吧

 
你应该先从新表中插入旧表中没有的记录,而后在修改旧表
 
先将newtable,newtable1,newtable2 用 select distinct ...join 一个没有重复记录的
大数据集(或 SQLSVR 的视图、ACCESS 的查询等)
再用 sunrainwang兄弟 的方法实现更新操作,具体语句随数据库类型的不同而有所区别
 
sunrainwang的方法应该差不多,一句应该不可能办到。
 
一句SQL办到真的很难,关注.
 
好象UPDATE有源数据集中找不到数据时可以插入数据的
 
to 死水:
有时候不要老早技术的阴影里面兜圈,
解决问题是关键,可一换一种方法实现,只要可以答到目的,
以我个人觉的这样的问题其实根本没有必要建老的表,
直接用临时表或者更简单一点就用一句SQL语句就可以实现了,
这是我个人的意见而已,只是在项目实现中的一些个人土办法,
也许对于学习SQL语句这样的建议是没有意义的
 
insert into oldtable (name,address) (selelct name ,address from newtable where not exist (newtabel.name in oldtable.name))
update oldtable set address = (select address from newtable where oldtable.name = newtable.name)
 
//删除旧表上的数据
delete from Oldtable
from Oldtable a,Newtable b
where a.name=b.name
//插入新表上的数据
insert into oldtable
select * from newtable

good luck!
 
一条语句:
Select IsNULL(A.name,B.Name),ISNULL(B.Address,A.Address) From OldTable A
FULL JOIN NewTable B
On A.Name=B.Name
祝你好运!
 
maolu28大侠,我照您的做可以,但是效率好像很低 因为我有好多表要导到oldtable中

其他的方法我还没试验,等试过了 马上给分 [:)]

如果liujunzhang大侠的方法可以那就最好不过了!
 
//更新旧表上的数据
update Oldtable
set Address=b.Address
from Oldtable a,Newtable b
where a.name=b.name
//删除已经更新的数据(新表)
delete from Newtable
from Oldtable a,Newtable b
where a.name=b.name
//插入新表上剩下的数据
insert into oldtable
select * from newtable

good luck!
 
*/. . . * . * 祝
./* . [] * .__ 你
? */ . .//~~~~~~~~~~~~'/. | ◆ 快
/* ,/,./,...........,/.| ◆ . 乐
|| ..▎# ▎田 田 ▎ | ▎◆ !
|| &&▎ ▎ ▎'|'▎ o ..
|| ##■■■■■■■■■■〓▄▃▂▁
祝事业有成!
 
先把table1,table2...用select distinct ...join 选出来
然后用batchmove来移动 没置好batchmove的属性
 
顶部