日期:2014-05-18  浏览次数:20558 次

求助一条两表关联的UPDATE语句;
table1  
id                   name
1                   张三
2                   李四
3                   王五
(此表ID号唯一,且不为空)


table2
id                 name
1                   空
空                 空
2                   空
2                   空
(此表ID列值不唯一,也有可能为空)

求助:如何将table2表中ID号与table1一致的name替换成table1表中的nmae,期望结果如下:
table2
id               name
1                 张三
空               空
2                 李四
2                 李四



------解决方案--------------------
update table2 set name=(select top 1 name from table1 where id=t.id)
from table2 as t
------解决方案--------------------
Update Table2
Set Name=Table1.Name
From Table1
Where Table2.id=Table1.id
------解决方案--------------------


update table2 set name=(select top 1 name from table1 where id=t.id)
from table2 as t
where id in (select id from table1)
------解决方案--------------------
Oracle?

最好还是去Oracle版问问,虽然以前用过Oracle,但都忘的差不多了
------解决方案--------------------
update table2 set name=(select top 1 name from table1 where id=t.id)
from table2 as t
where id in (select id from table1)
------解决方案--------------------
--oracle的两表相连update的语句.

update aa p
set aa_code=(
select c.aa_code
from "bb " r, cc c
where p.bb_code = r.bb_code
and r.name = c.name
)
where exists (
select c.aa_code
from "bb " r, cc c
where p.bb_code = r.bb_code
and r.name = c.name
)