开发者

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

开发者 https://www.devze.com 2023-02-22 08:42 出处:网络
I have two tables, A and B. I\'d like to select a row from B based on it\'s Primary Key, alter the primar开发者_如何转开发y key to a different value, and then insert this row into A.

I have two tables, A and B.

I'd like to select a row from B based on it's Primary Key, alter the primar开发者_如何转开发y key to a different value, and then insert this row into 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


Maybe something like

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

精彩评论

暂无评论...
验证码 换一张
取 消