简体   繁体   中英

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".
This is stored in table 1.

Now i'd like to insert the data "cool" into table 2 WHERE number is "5".

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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