Mysql中通過關聯update將一張表的一個字段更新到另外一張表中


接着之前寫的一篇文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;  

 

將一張表的查詢結果插入到另外一張表中

insert select :將一條select語句的結果插入到表中

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

  

本篇文章如有幫助到您,請給「翎野君」點個贊,感謝您的支持。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM