0
点赞
收藏
分享

微信扫一扫

Mysql中通过关联update将一张表的一个字段更新到另外一张表中


做什么事情

更新book_borrow表,设置其中的student_name为student表中的name,关联条件为book_borrow.student_id = student_id

student表

Mysql中通过关联update将一张表的一个字段更新到另外一张表中_表数据

book_borrow表

Mysql中通过关联update将一张表的一个字段更新到另外一张表中_表名_02

几种不同的更新方式

保留原表数据的更新

只会更新student表中有的数据,student表中查不到的数据,在book_borrow表中还保持不变,不会更新,相当于内连接

update book_borrow br,student st set br.student_name = st.name where br.student_id = st.id;

 

Mysql中通过关联update将一张表的一个字段更新到另外一张表中_数据_03

全部以右表数据为准

更新结果以student的查询结果为准,student中没有查到的记录会全部被更新为null 相当于外连接

update book_borrow br set student_name = (select name from student where id = br.student_id);
update book_borrow br left join student st on br.student_id = st.id set br.student_name = st.name;

Mysql中通过关联update将一张表的一个字段更新到另外一张表中_数据_04

 

将一张表的查询结果插入到另外一张表中

insert select :将一条select语句的结果插入到表中

-- insert into 表名1 (列名) select (列名) from 表名2 ;
insert into tableA(columnA) select columnA from tableB where id=1

  

本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。


Mysql中通过关联update将一张表的一个字段更新到另外一张表中_数据_05

作者:翎野君

如果您喜欢或希望看到更多我的文章,可关注我的微信公众号《翎野君》。

转载文章请务必保留出处和署名,否则保留追究法律责任的权利。



举报

相关推荐

0 条评论