复制行及其所有' children' [英] Copy row and all of its 'children'
问题描述
我有一个名为employees
的MySQL表.还有一个名为shifts
的表,该表通过employeeId
列与一对多关系绑定到employees
.我想使用INSERT
克隆一名员工及其班次-因此克隆了employee
行,并且所有shifts
都被克隆了,并具有为新的employee
生成的新的自动递增主键.一个查询有可能吗?
I have a MySQL table called employees
. There's another table called shifts
that is tied to employees
by the employeeId
column and a one-to-many relationship. I want to clone an employee and his shifts using INSERT
- so the employee
row is cloned, and the shifts
are all cloned and have the new auto-incrementing primary key that was generated for the new employee
. Is this possible in one query?
推荐答案
是的,但不能一次插入.
Yes but not with a single insert.
您将需要一个INSERT来处理员工,然后再使用第二个INSERT来进行子选择,以选择所有班次数据.
You'd have one INSERT to do the employee, then a second INSERT with a sub-SELECT to SELECT all of the shift data.
以下是一些示例
这篇关于复制行及其所有' children'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!