将日期时间值与 null 进行比较时日期时间值不正确
Incorrect datetime value while comparing a datetime value with null
我的数据库中有两个日期时间列 (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)
您可以将 whereNotNull
用于 not null
条件。
您应该使用 whereDate
而不是 where
进行日期比较。请通过 https://laravel.com/docs/5.8/queries#where-clauses 了解更多。
问题是您将 due_date
列与 completed_at
STRING 进行比较,而不是此列的值。所以改变这一行:
->where('due_date' ,'>=', 'completed_at')
对此:
->whereColumn('due_date' ,'>=', 'completed_at')
请参阅 documentation(whereColumn / orWhereColumn
部分)
让我解释一下。
... and `due_date` >= completed_at
^
this is not a column, but plain string value
这就是您收到错误的原因。
所以,你可以选择:
->where('column_name', '>', DB::raw('other_column_name'))
或
->whereRaw('column_name > other_column_name')
哦,好吧,原来我是个白痴。 Laravel 中的 where 子句不比较两列,而我应该使用 whereColumn。早些时候,它比较列 'due_date' 和字符串 'completed_at' 的值。现在我的新密码是-
$completed_tasks_before = Task::where('assignee_id', $current_user->id)
->where('status','completed')
->whereColumn('due_date', '>=', 'completed_at')
->count();
我的数据库中有两个日期时间列 (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)
您可以将 whereNotNull
用于 not null
条件。
您应该使用 whereDate
而不是 where
进行日期比较。请通过 https://laravel.com/docs/5.8/queries#where-clauses 了解更多。
问题是您将 due_date
列与 completed_at
STRING 进行比较,而不是此列的值。所以改变这一行:
->where('due_date' ,'>=', 'completed_at')
对此:
->whereColumn('due_date' ,'>=', 'completed_at')
请参阅 documentation(whereColumn / orWhereColumn
部分)
让我解释一下。
... and `due_date` >= completed_at
^
this is not a column, but plain string value
这就是您收到错误的原因。
所以,你可以选择:
->where('column_name', '>', DB::raw('other_column_name'))
或
->whereRaw('column_name > other_column_name')
哦,好吧,原来我是个白痴。 Laravel 中的 where 子句不比较两列,而我应该使用 whereColumn。早些时候,它比较列 'due_date' 和字符串 'completed_at' 的值。现在我的新密码是-
$completed_tasks_before = Task::where('assignee_id', $current_user->id)
->where('status','completed')
->whereColumn('due_date', '>=', 'completed_at')
->count();