[636]mysql update多表联合更新

下面我建两个表,并执行一系列sql语句,仔细观察sql执行后表中数据的变化,很容易就能理解多表联合更新的用法

[636]mysql update多表联合更新_第1张图片

  1. 执行 UPDATE student s,class c SET s.class_name='test00',c.stu_name='test00' WHERE s.class_id = c.id
    [636]mysql update多表联合更新_第2张图片

  2. 执行UPDATE student s JOIN class c ON s.class_id = c.id SET s.class_name='test11',c.stu_name='test11'
    [636]mysql update多表联合更新_第3张图片

  3. 执行 UPDATE student s LEFT JOIN class c ON s.class_id = c.id SET s.class_name='test22',c.stu_name='test22'
    [636]mysql update多表联合更新_第4张图片

  4. 执行 UPDATE student s RIGHT JOIN class c ON s.class_id = c.id SET s.class_name='test33',c.stu_name='test33'
    [636]mysql update多表联合更新_第5张图片

  5. 执行 UPDATE student s JOIN class c ON s.class_id = c.id SET s.class_name=c.name , c.stu_name=s.name
    [636]mysql update多表联合更新_第6张图片
    6、其它类似sql

UPDATE b SET b.memo = a.name FROM a, b WHERE a.id = b.id
UPDATE b SET b.memo = a.name FROM b LEFT JOIN a ON a.id = b.id
UPDATE A  SET A1 = B1, A2 = B2, A3 = B3  FROM A LEFT JOIN B ON A.ID = B.ID
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A, B WHERE A.ID = B.ID

参考:https://blog.csdn.net/u012604745/article/details/80642015
https://blog.csdn.net/lxrj2008/article/details/79457804

你可能感兴趣的:(数据库&中间件)