【发布时间】:2015-08-20 05:54:04
【问题描述】:
我有一张桌子total_count
+----+--------+-------+------+---------+---------+---------+
| id | studid | month | year | acls_id | total_p | total_a |
+----+--------+-------+------+---------+---------+---------+
| 1 | 30 | 08 | 2015 | 12 | 5 | 2 |
| 2 | 35 | 08 | 2015 | 12 | 5 | 2 |
| 3 | 52 | 08 | 2015 | 12 | 5 | 2 |
| 4 | 53 | 08 | 2015 | 12 | 5 | 2 |
| 5 | 54 | 08 | 2015 | 12 | 5 | 2 |
| 6 | 55 | 08 | 2015 | 12 | 5 | 2 |
| 7 | 30 | 09 | 2015 | 12 | 3 | 0 |
| 8 | 35 | 09 | 2015 | 12 | 3 | 0 |
| 9 | 52 | 09 | 2015 | 12 | 2 | 1 |
| 10 | 53 | 09 | 2015 | 12 | 3 | 0 |
| 11 | 54 | 09 | 2015 | 12 | 3 | 0 |
| 12 | 55 | 09 | 2015 | 12 | 3 | 0 |
+----+--------+-------+------+---------+---------+---------+
我想为total_p 和total_a 的每个学生递增和递减。
当我编辑我的学生出勤列表时。
例如: studid 30 total_p = 5 and total_a= 2 ,所以我编辑我的出席出席变成缺席。
所以想将 total_p 减 1 并将 total_a 加 1。
所以我想获得每个 studid 的每个月的总和,以及总月数的 total_p 和 total_a 的增量和减量。
我的控制器代码是
foreach ($student as $student) {
if ($present == 0) {
$query = DB::table($wys_total_attend_table)
->where('studid', $student->id)
->where('smonth', '=', $date_exploded[1])
->where('syear', '=', $date_exploded[2])
->update([
'stotal_p' => DB::raw('stotal_p - 1'),
'stotal_a' => DB::raw('stotal_a + 1'),
]);
} elseif ($present == 1) {
$query = DB::table($wys_total_attend_table)
->where('studid', $student->id)
->where('smonth', '=', $date_exploded[1])
->where('syear', '=', $date_exploded[2])
->update([
'stotal_p' => DB::raw('stotal_p + 1'),
'stotal_a' => DB::raw('stotal_a - 1'),
]);
}
}
但它不起作用..
如何在查询生成器格式中使用increment() 和decrement()?
例如:如果我只编辑 studid = 30 出勤增量 total_p 值 1 和(当前 == 1) studid = 30 total_p = 6 和 total_a = 1 和其他 studid 值是旧值。
【问题讨论】:
-
你确定你的SQL逻辑正确吗?
-
对不起,我不知道......但我想要这样......如果(present==1)然后增加(stotal_p,1)和减少(stotal_a,1)和if(present== 0) 然后在条件为真的情况下递增 (stotal_a,1) 和递减 (stotal_p,1)。
-
将您的构建器代码转换为 MySQL 查询并针对出勤表运行。
-
如果你不介意..我的逻辑是错误的......你能告诉我什么是正确的代码吗??
-
先生,我不想将我的构建器代码转换为 mysql....如何在查询构建器中使用增量()和减量()??
标签: php laravel laravel-4 eloquent