Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想根据外键复制 mysql 表中的几行,并为新行分配一个新的外键 ID。假设我的表格布局如下所示:
test ----- table1_id int(11) value varchar(20)
test
-----
table1_id int(11)
value varchar(20)
我该如何做到这一点?
发现查询需要如下所示: INSERT INTO test (table1_id, value) (SELECT '2', value FROM test WHERE table1_id=1)
INSERT INTO test (table1_id, value) (SELECT '2', value FROM test WHERE table1_id=1)
这将复制具有外键 ID '1' 的所有行,并改为分配具有 ID 2 的新行。如果表包含更多行,您可以在 SELECT 部分添加它们或更改顺序,如下所示 ...(SELECT row1, '{$new_id}', value, another_row FROM...
...(SELECT row1, '{$new_id}', value, another_row FROM...