MySQL中如何用一个表中的字段更新另一个表中字段

1,修改1列

update student s, city c
set s.city_name = c.name
where s.city_code = c.code;

2,修改多个列

update  a,  b
set a.title=b.title, a.name=b.name
where a.id=b.id
  • 子查询
update student s set city_name = (select name from city where code = s.city_code);

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

oracle查询报这个错误:single-row subquery returns more than one row怎么解决? 

数据库按照你的条件查询有多个重复的数据。

例如:

UPDATE "SYS_ROLE" A

SET A ."DEPT_ID" = (

    SELECT

        c."id"

    FROM

        "his_department_info" c

    WHERE

        c."dept_name" = A ."ROLE_NAME"

如果以上sql语句报single-row subquery returns more than one row的错误,说明 c表”dept_name” 和A 表.”ROLE_NAME” 的这两个字段 数据重复

2、 写法轻松,更新效率高:

update table1 
set field1=table2.field1,
field2=table2.field2
from table2
where table1.id=table2.id

猜你喜欢

转载自blog.csdn.net/shujuelin/article/details/83819317
今日推荐