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

3次阅读

共计 588 个字符,预计需要花费 2 分钟才能阅读完成。

接着之前写的一篇文 https://www.cnblogs.com/lingyejun/p/11915413.html

做什么事件

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

student 表

book_borrow 表

几种不同的更新形式

保留原表数据的更新

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

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

全副以右表数据为准

更新后果以 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;  

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

正文完
 0