update set ... select ... 如何写sql

来源:百度知道 编辑:UC知道 时间:2024/06/21 10:52:04
我现有两个表A,B ,是access数据库

A 表的结构
ID,username,fen

B表的结构
ID,username,fen
其中id是自动编号,其他的属性都是文本属性
现在我想选出A表中username和B表中username相同的记录行出来,并且比较两个表的fen这个属性项,若是fen这个属性项也相同的话,则不对记录进行任何操作,若是不同的化,要把A表中fen这个属性值更新到B表中与A表username相同的项,反正就是我要更新B表,更新过后,A表中username和B表中username相同的项,fen这个属性也要相同
用 update set ... select ... 如何写sql
能把那些代号和我的对应下吗?一下子搞不清楚,呵呵,本来就完全不懂这些东西,还有这种方式对access数据库也可以用吗?

-----------------------------------------
--Update 与 Select 一起的用法
--Update 通过多表来修改数据
-----------------------------------------

--参考写法1:
update B1 set strNum = strNum - B2.intNum
where strId in
(select intId from B2 where B2.intId = B1.strId)

--参考写法2:
update B1 set strNum = strNum - B2.intNum
from B1 inner join B2
on B2.intId = B1.strId

update tablename set t2.fen=t1.fen from tablename t1 join tablename t2
on t1.username=t2.username where t1.fen<>t2.fen