简体   繁体   English

使用JOIN将Identical Id列插入第二张表插入第一张表

[英]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.

相关问题 根据第一个表列与第二个表联接 - Join with second table based on first table column 当第二个表的 id 依赖于第一个表时,使用 php 一次插入两个表 - Insert two table at a time mysql using php when id of second table is depend on first table MySQL 如果 table1.id = table2.table1_id 查询应该在第一个表中插入第二个表 - MySQL Query should insert a second table into the first table if table1.id = table2.table1_id 使用更新查询按第二个表的第一列的列更新 - update column of second table by column of first table using update query 使用第一个表ID将数据插入2个表 - Insert data into 2 tables using first table ID MySQL Join Query,无法访问第一个表id列 - MySQL Join Query, unable to access first table id column 加入 2 个表,将第一个表添加为 datagridview 的列标题,然后将第二个表添加为列标题下方的行 - Join 2 tables, add first table as column headers for datagridview then add second table as rows below column headers 使用第一个表的自动递增的ID更新第二个表 - update second table with the autoincremented id of the first table 如果日期介于第二个表的日期之间,则使用第一个表中的日期从第二个表中获取列 - Fetch Column from second table using date from first table if the date is in between dates from second table 如何获取连接表中第一个表的ID - How to get ID of the first table in a join table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM