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

求一更新sql语句
现有2表A   B
A   表
id   value
1       a
2       3
3       5
4       f
5       5

B   表
id   value
1       a
3       c
5       e


现在我想把B表中的value更新到A表相应的id之下,应该怎么做呢?
如果B表存在id> 6的数据A表能自动添加吗?

------解决方案--------------------
update A set Value = B.Value from B where A.id=B.id
------解决方案--------------------

现在我想把B表中的value更新到A表相应的id之下,应该怎么做呢?
==>
update A set Value = B.Value from B where A.id=B.id

如果B表存在id> 6的数据A表能自动添加吗?
\==>
不能,可以写个触发器实现

------解决方案--------------------
同楼上
> 6 没关系 数据一样更新
------解决方案--------------------
现在我想把B表中的value更新到A表相应的id之下,应该怎么做呢?
---------------------------------
update a
set value=b.value
from a inner join b on a.id=b.id
------解决方案--------------------
我试了一下> 6是不能更新的,我觉得是不是写个not in 的insert会好一点?
update A set Value = B.Value from B where A.id=B.id
insert into A(id,Value)
select id, Value from B where id not in (select id from A) and id > 6