MySQL: Select row by id, CHANGE id, and insert it into another table
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")
精彩评论