[英]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_address
和tbl_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 insert
并insert
到tbl_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.