从特定的开始显式插入自动增量列号

穆罕默德·费赞汗 |

我试图在同一个表中复制一些数据,但愿意从特定的数字序列中添加一个 id 列。记住 PK(id 列)是自动递增的。

INSERT INTO `switch_person` (PK, `PTPK`, `EmployeeNamePK`, `SwitchTime`, `Half`, `date`, `Month`, `Year`, `SwitchDate`, `hoursWorked`, `LeadBy`, `Info`, `EmpDaysStatusId`, `TaskPK`, `TaskAssignCompletionId`, `SpendDays`, `LeaveId`, `Comments`)
select [Number will be here with each record increment], `PTPK`, `EmployeeNamePK`, `SwitchTime`, 1, `date`, `Month`, `Year`, `SwitchDate`, `hoursWorked`, `LeadBy`, `Info`, `EmpDaysStatusId`, `TaskPK`, `TaskAssignCompletionId`, `SpendDays`, `LeaveId`, `Comments`
from switch_person
where SwitchDate = '2021-08-17'
and Half = 2

就像我想用 687477 号添加 PK,下一条记录应该添加 687478 然后是 687479

拉斐尔小

首先将增量设置为 687477

ALTER TABLE switch_person AUTO_INCREMENT=687477;

然后运行您的插入,不指定 pk 它将使用自动增量并为下一次插入增加它。

INSERT INTO `switch_person` (`PTPK`, `EmployeeNamePK`, `SwitchTime`, `Half`, `date`, `Month`, `Year`, `SwitchDate`, `hoursWorked`, `LeadBy`, `Info`, `EmpDaysStatusId`, `TaskPK`, `TaskAssignCompletionId`, `SpendDays`, `LeaveId`, `Comments`)
select `PTPK`, `EmployeeNamePK`, `SwitchTime`, 1, `date`, `Month`, `Year`, `SwitchDate`, `hoursWorked`, `LeadBy`, `Info`, `EmpDaysStatusId`, `TaskPK`, `TaskAssignCompletionId`, `SpendDays`, `LeaveId`, `Comments`
from switch_person
where SwitchDate = '2021-08-17'
and Half = 2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章