mysql - 从特定开始显式插入自动增量列号
问题描述
我正在尝试在同一个表中复制一些数据,但愿意从特定的数字序列中添加一个 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
推荐阅读
- openrefine - 在 OpenRefine 的大文本中删除某些行中字符之前的所有内容
- three.js - 3D 模型中的三框工具提示
- common-lisp - 如何将动态生成的 Lisp 系统保存在外部文件中?
- java - 我是否需要端口转发才能在同一网络上的两台机器之间进行通信?
- amazon-rds - 设置 Amazon RDS 数据库的推荐安全设置是什么
- azure-storage - Azure Blob 存储同步:获取接触文件的列表
- javascript - 猫鼬总是保存错误的日期
- flutter - 下面的构造函数在 dart 中是如何工作的,我已经提取了小部件,flutter 为我的小部件提供了下面的构造函数
- java - Javadoc {@value} 不适用于常量
- javascript - 在登录 nodejs 上管理会话