数据库有两个表,表1和表2,结构如下
表1
bianma jiage
111AADDD 111
222ffg 21
3434ffggg 34
表2
Partsnum Partspri
111AADDD
122dfdf
2d232323
我想用表1的“bianma”与表2“partsnum”进行比对,如果有相同则用表1的“jiage”替换表2的“partspri”。寻求这样的SQL语句,多谢各位朋友,帮帮忙了。
------解决方案--------------------
update B set b.partspri=a.jiage from A JOIN B ON a.bianma=b.partsnum
------解决方案--------------------
- SQL code
update 表2set partspri =表1.jiagefrom 表1where 表1.bianma = 表2.partsnum
------解决方案--------------------
- SQL code
create table #a (bianma varchar(20),jiage int )create table #b(Partsnum varchar(20),Partspri int)--drop table #b--drop table #ainsert into #a select '111AADDD',111 UNION ALLselect '222ffg',21 UNION ALLselect '3434ffggg',34insert into #b select '111AADDD',0 UNION ALLselect '1222dfdf',0 UNION ALLselect '2d232323',0select * from #aselect * from #bupdate b set b.Partspri=a.jiage from #a a,#b b where a.bianma=b.partsnum--结果:Partsnum Partspri111AADDD 1111222dfdf 02d232323 0