简体   繁体   中英

Insert data from one column to another MYSQL based on row

I am new to MYSQL queries so I am struggling with this.

I have two tables

Table 1 

  id       phone1  phone2   name     ...
  1        123      456
  3        234      567
  7        345      678

Table 2

  id        p1        p2    age     ...
  1        1123      2456
  7        1345      2678
  3        1234      2567

ID is the same for both tables. Both tables have many other rows.

I want to copy the data from Table 2 to Table 1 such that the id stays the same.

So the output should be

Table 1 
  id       phone1  phone2   name
  1        1123      2456
  3        1234      2567
  7        1345      2678

Already answered :

stackoverflow

In your case :

UPDATE table1 t1
    INNER JOIN table2 t2 ON t2.id = t1.id
    SET t1.phone1 = t2.p1,
    t1.phone2 = t2.p2;

Try This.

UPDATE table1 tbl1
JOIN table2 tbl2 
ON tbl2.id = tbl1.id
SET tbl1.phone1 = tbl2.p1,
tbl1.phone2 = tbl2.p2;
UPDATE Table1 tab1
    INNER JOIN Table2 tab2 ON tab2.id = tab1.id
    SET 
    tab1.Column2 = tab2.Column3;

Check this sqlfiddle

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