我有一个名为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.
以下是一些示例
更多推荐
复制行及其所有“子级"
发布评论