查询生成器 returns 不正确的日期时间比较查询结果
query builder returns incorrect datetime compare query results
我有这个查询:
return DB::table('electricity_meters')
->where('electricity_meters.id', 16)
->leftJoin('service_instances', 'service_instances.electricity_meter_id', '=', 'electricity_meters.id')
->leftJoin('readings', 'electricity_meters.kwh_param_id', '=', 'readings.parameter_id')
->where('readings.taken_at', '>', 'service_instances.active_to')
->select([
'readings.taken_at',
'service_instances.active_to',
])
->get();
关注'readings.taken_at', '>', 'service_instances.active_to'
,但不知何故结果...
[
{
taken_at: "2021-02-16 23:59:59",
active_to: "2021-03-28"
},
...
]
我在这里错过了什么?
我想你想比较两列,如果那样的话,你应该使用 whereColumn:
return DB::table('electricity_meters')
->where('electricity_meters.id', 16)
->leftJoin('service_instances', 'service_instances.electricity_meter_id', '=', 'electricity_meters.id')
->leftJoin('readings', 'electricity_meters.kwh_param_id', '=', 'readings.parameter_id')
->whereColumn('readings.taken_at', '>', 'service_instances.active_to')
->select([
'readings.taken_at',
'service_instances.active_to',
])
->get();
我有这个查询:
return DB::table('electricity_meters')
->where('electricity_meters.id', 16)
->leftJoin('service_instances', 'service_instances.electricity_meter_id', '=', 'electricity_meters.id')
->leftJoin('readings', 'electricity_meters.kwh_param_id', '=', 'readings.parameter_id')
->where('readings.taken_at', '>', 'service_instances.active_to')
->select([
'readings.taken_at',
'service_instances.active_to',
])
->get();
关注'readings.taken_at', '>', 'service_instances.active_to'
,但不知何故结果...
[
{
taken_at: "2021-02-16 23:59:59",
active_to: "2021-03-28"
},
...
]
我在这里错过了什么?
我想你想比较两列,如果那样的话,你应该使用 whereColumn:
return DB::table('electricity_meters')
->where('electricity_meters.id', 16)
->leftJoin('service_instances', 'service_instances.electricity_meter_id', '=', 'electricity_meters.id')
->leftJoin('readings', 'electricity_meters.kwh_param_id', '=', 'readings.parameter_id')
->whereColumn('readings.taken_at', '>', 'service_instances.active_to')
->select([
'readings.taken_at',
'service_instances.active_to',
])
->get();