我有一个带有自动增量ID的表。
我必须根据插入之前的id生成序列
我可以找到最高的ID值,但是如果之前有删除,并且自动增量
表!= max(id)+1
最佳答案
将其分为两个查询。
运行插入查询来检索 ID。
根据检索到的 ID 创建序列。
使用更新查询将序列值添加到记录中。
使用 ORM 模块的示例代码
$my_table = ORM::factory('Mytable');
$my_table->field1 = 'Value1';
$my_table->field2 = 'Value2';
$my_table->save(); // inserts record
// Create serials (will obviously be more complex than this)
$my_table->serial = $my_table->id;
$my_table->save(); // updates record
使用数据库模块的示例代码
list($id) = DB::insert('my_table', array('field1','field2'))
->values(array('value1','value2')
->execute();
// Create serials (will obviously be more complex than this)
$serial = $id;
DB::update('my_table')
->set(array('serial' => $serial))
->where('id', '=', $id);
关于mysql - Kohana DB 插入前最后一次插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880322/