[英]Using JOIN to INSERT INTO second Table with an Identical Id column as the first Table
I have a Database (MySQL) that contains two tables with identical primary keys, I have already inserted data into the first table but I want to insert data into the second table at a later time. 我有一个数据库(MySQL),其中包含两个具有相同主键的表,我已经在第一个表中插入了数据,但是我想稍后再将数据插入到第二个表中。
The logic I've got is: 我得到的逻辑是:
[INSERT into Table2 (col1, col2, col3) Values (X1,X2,X3) WHERE Table1.id equals Table2.id]
I can't seem to find anything that works like this for an Insert Statement in use with PHP. 对于与PHP配合使用的Insert语句,我似乎找不到任何能像这样工作的东西。
Any help appreciated! 任何帮助表示赞赏!
It sounds like you really want to update the second table, not insert into it. 听起来您确实想更新第二个表,而不是插入其中。
INSERT
is for creating new rows, UPDATE
is for modifying existing rows. INSERT
用于创建新行, UPDATE
用于修改现有行。
You do this by using a JOIN
in an UPDATE
statement. 您可以通过在
UPDATE
语句中使用JOIN
来实现。
UPDATE Table2 AS t2
JOIN Table1 AS t1 ON t1.id = t2.id
SET t2.col1 = t1.x1, t2.col2 = t1.x2, t2.col3 = t1.x3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.