【发布时间】:2016-04-21 01:42:56
【问题描述】:
我将 Managers、Course_Type 和 Course Years 存储在数据库中。
我将根据递增值命名我的课程,例如
Course_type course_order course_year
Course_type[0] 1 2015
Course_type[0] 2 2015
Course_type[0] 3 2015
Course_type[0] 4 2015
Course_type[0] 1 2016
Course_type[1] 1 2015
Course_type[2] 1 2015
Course_type[2] 2 2015
所以我创建了类似的数据库
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->enum('course_type', [
'Yos',
'TomerABC',
'TomerA1A2',
'TomerB1B2',
'TomerC1C2',
'TomerAB',
'TomerBC',
'SatMatGeo',
'SatCriRea',
'SatCriReaMat',
]);
$table->integer('added_by');
$table->integer('start');
$table->integer('year_code');
$table->integer('end');
$table->integer('course_order');
$table->unique( array('added_by','year_code','course_order') );
这是我在 createprocess 上的控制器
$courseorder = \App\Courses::where('added_by', \Auth::id())
->where('year_code', $input['year_code'])->max('course_order');
$course->course_order = $courseorder + 1;
但由于我在数据库中查找 max('course_order'),如果我删除,可以说课程顺序 2。下一个添加的课程将是 4 而不是 3。
所以我的问题是如何检查 course_order 之间的缺失值以在删除一个位置后填充位置?
P.S 一个 mysql 解决方案会比我认为的 php 更快更好。
【问题讨论】:
-
这是一个常见的 SQL 问题,用于搜索缺失的数字。我会采用计数表方法。 Some info here.
标签: php mysql sql laravel eloquent