简体   繁体   English

MySQL从一个表中选择记录并添加到表2中,将最后插入的ID更新到表1中

[英]Mysql select record from one table and add into table 2, update last inserted ID into table1

I have two different tables tbl_address and tbl_support . 我有两个不同的表tbl_addresstbl_support I want to select address , postcod e from tbl_support and insert into into tbl_address and want to update address_id in tbl_support with last inserted id . 我想选择address ,从tbl_support postcod e insertinserttbl_address并希望使用最后插入的id update tbl_support address_id How do I write query. 我该如何写查询。

tbl_address = id, address, postcode, country.
tbl_support = id, address_id ,name, address,postcode,

if your column id field in address table autoincrement try this: 如果地址表中的列ID字段自动递增,请尝试以下操作:

   insert into tbl_address select address, postcode from tbl_support;

then you can update tbl_support if address and postcode are unique set. 如果地址和邮政编码是唯一的,则可以更新tbl_support。

 update tbl_support s set s.address_id=(select id from tbl_address a where 
 a.address=s.address and a.postcode = s.postcode) 

you need to execute to two separate queries. 您需要执行两个单独的查询。 first you insert all tbl_support record in tbl_address table.then you need to execute second query where you update address_id through a matching tbl_address address,postcode and tbl_support address, zip code.like below query. 首先在tbl_address表中插入所有tbl_support记录,然后需要执行第二个查询,在该查询中,通过匹配的tbl_address地址,邮政编码和tbl_support地址,邮政编码(如下面的查询)来更新address_id。

INSERT INTO tbl_address ( 
      address, 
      postcode) 
SELECT address, 
       postcode 
FROM `tbl_support`;

UPDATE `tbl_support` SET address_id = (SELECT id FROM `tbl_address` 
WHERE  tbl_support.address= tbl_address.address 
AND tbl_support.postcode= tbl_address.postcode)

暂无
暂无

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

相关问题 INSERT INTO table2 SELECT FROM table1然后UPDATE table1已选择/插入的行 - INSERT INTO table2 SELECT FROM table1 then UPDATE table1 rows that selected/inserted MYSQL:来自table1的SELECT 1,其中Id = 1 vs SELECT EXISTS(来自table1的SELECT 1,其中Id = 1) - MYSQL: SELECT 1 from table1 where Id = 1 vs SELECT EXISTS(SELECT 1 from table1 where Id = 1) 需要MySQL查询以将记录从table2更新/添加到table1 - Need MySQL query to update/add record into table1 from table2 从mysql表中获取最后插入的ID - Get last inserted ID from mysql table 在 mysql 中选择 table1 和 table2 中的所有结果,其中两个表都有一个公共 ID - Select all results from table1 as well table2 in mysql where both table has one common id MySQL从表1中选择ID并从表2中选择计数(ID) - Mysql select id from table1 and select count(id) from table2 如果table2有更新,则更新table1中的记录(MySQL) - Update a record in table1 if table2 has updates (MySQL) MySQL:如果表2中表1中的记录计数为0或1,则将其添加到表3中 - MySQL: Add to table3 if record from table1 has a count of 0 or 1 in table 2 MySQL从表1中检索最后一条记录,并从表2中检索数据 - MySQL Retrieving last record from table1 and join data from table 2 java,mysql-为什么不工作,从一个表中获取最后一个插入的ID,然后将其再次插入另一个表中? - java,mysql - why not working get last inserted id from one table and insert it again on another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM