1. 程式人生 > >mysql實現從複製某個表內的某一欄位到另外一個表的方法

mysql實現從複製某個表內的某一欄位到另外一個表的方法

在寫sql的過程中,出現一個需要把某表的的某列資料全部更新到另一張表的某列中的需求,然後查了下資料,得到了答案,

所以總結記錄一下。

方法一:更新某一列到另一列

update table1 a,table2 b set a.field1=b.field2  where a.field3=b.field4

方法二:更新多列

update table1 a,table2 b set a.field1=b.field2,a.field3=b.field4  where a.field5=b.field6

方法三:子查詢的方法更新

update table1 set field1=(select filed2 from  table2)

方法四:另附一種網上查到的聚合查詢的方法

update table1 a inner join table2 b on a.field1=b.field2 set a.field3=b.field4,a.field5=b.field6;