复制行及其所有' children' [英] Copy row and all of its 'children'

查看:112
本文介绍了复制行及其所有' 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆