我的数据库(mysql)中有两个日期时间列-completed_at、due_date。我正在使用连接到 mysql 的流明框架。问题是我的 'completed_at' 列可以为空并且包含空值。现在我需要计算有多少记录在其due_date 之前完成了工作,如果它们的'completed_at' 不为空。
我的代码是-
$completed_tasks_before = Task::where('tasks.assignee_id', $current_user->id)
->where('status','completed')
->where('completed_at', '<>', null)
->where('due_date' ,'>=', 'completed_at')
->count();
我希望这可以为我提供due_date >= completed_at 的记录计数,但它给了我以下错误-
SQLSTATE[HY000]: General error: 1525 Incorrect DATETIME value: 'completed_at' (SQL: select count(*) as aggregate from `tasks` where `tasks`.`assignee_id` = 2 and `status` = completed and `completed_at` is not null and `due_date` >= completed_at)
问题是您将该due_date
列与completed_at
STRING进行比较,而不是该列的值。所以改变这一行:
->where('due_date' ,'>=', 'completed_at')
对此:
->whereColumn('due_date' ,'>=', 'completed_at')
请参阅文档(whereColumn / orWhereColumn
部分)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句