a,b两个表,通过sql连接后,主键相同的记录,将b表的某个字段更新到a表的某个字段,sql语句怎么写?

a表字段:code, name(主键)

b表字段:num, name(主键), age

如何将a表和b表通过主键连接后,然后将age字段更新到code字段上?

update a set code=age
from a,b
where a.name=b.name

祝你成功!

追问

请问:如果b表的name是”计算机(数据库)“这种形式,而a表的name是”计算机“这种形式,我该怎么去掉"(数据库)",然后使2个表连接?

追答

试试:

update a set code=age
from a,b
where left(a.name,3)=b.name

祝你成功!

追问

。。。。。那个“计算机”只是个案例,比如计算机科学(xxxx),电子商务(xxxxxxx)这样的。。。

追答

试试:

update a set code=age
from a,b
where a.name like b.name + '%'

祝你成功!

追问

我采用了如下方法:

不知是否有问题??

追答

可以,这样更精确。

温馨提示:答案为网友推荐,仅供参考
相似回答