[英]MySql: insert into table SELECT from another table where first_table ID =another_table.id
[英]MySQL: Select row by id, CHANGE id, and insert it into another table
我有兩個表,A和B。
我想根據其主鍵從B中選擇一行,將主鍵更改為其他值,然后將該行插入A。
像這樣:
INSERT INTO A SELECT id, title, date FROM B WHERE B.id="4" UPDATE B SET id="5";
有解決方案嗎?
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.