简体   繁体   English

MySql:使用WHERE子句将数据从一个表复制到另一个表

[英]MySql: Copy data from one table to another with WHERE clause

I'd like to copy data from one table to another. 我想将数据从一个表复制到另一个表。

I'd like to copy some data, let's say: "Andy". 我想复制一些数据,比如说:“安迪”。 His number is "5" and his data is "cool". 他的数字为“ 5”,其数据为“酷”。
This is stored in table 1. 这存储在表1中。

Now i'd like to insert the data "cool" into table 2 WHERE number is "5". 现在,我想将数据“ cool”插入表2中,其中数字为“ 5”。

SQL 的SQL

INSERT TO table2 SET data = (SELECT data FROM table1 WHERE number = table2.number)

So, this should copy data from multiply users, like a loop. 因此,这应该像循环一样从多个用户复制数据。

How should i do this? 我应该怎么做?

If you need to update the values of Table2 from values of Table1, you can use update statement with joins. 如果需要从Table1的值更新Table2的值,则可以将update语句与联接一起使用。

UPDATE  Table2
JOIN Table1  ON Table1.number = Table2.number
SET Table2.data = Table1.data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM