简体   繁体   English

根据行将数据从一列插入另一MYSQL

[英]Insert data from one column to another MYSQL based on row

I am new to MYSQL queries so I am struggling with this. 我是MYSQL查询的新手,所以我为此感到挣扎。

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. 两个表的ID相同。 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. 我想将数据从表2复制到表1,以使ID保持不变。

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 检查这个sqlfiddle

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

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