简体   繁体   English

MySQL:按ID选择行,更改ID,然后将其插入另一个表

[英]MySQL: Select row by id, CHANGE id, and insert it into another table

I have two tables, A and B. 我有两个表,A和B。

I'd like to select a row from B based on it's Primary Key, alter the primary key to a different value, and then insert this row into A. 我想根据其主键从B中选择一行,将主键更改为其他值,然后将该行插入A。

Something like this: 像这样:

INSERT INTO A SELECT id, title, date FROM B WHERE B.id="4" UPDATE B SET id="5";

Is there a solution for this? 有解决方案吗?

INSERT INTO A(id, title, date)
SELECT 5 AS id, title, date FROM B WHERE B.id="4";
#      ^--- here is the trick

也许像

insert into a (select id as "new_id", title, date from b where b.id="4")

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

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