Laravel连接两个表,其中比较两个日期时间列
我有两个表,我想使用两个日期时间列的值对其运行联合查询,我有products表和sync_status表,我想返回所有在日期时间更新的_大于在日期时间同步的_的产品Laravel连接两个表,其中比较两个日期时间列,laravel,eloquent,laravel-eloquent,Laravel,Eloquent,Laravel Eloquent,我有两个表,我想使用两个日期时间列的值对其运行联合查询,我有products表和sync_status表,我想返回所有在日期时间更新的_大于在日期时间同步的_的产品 DB::table('products') ->join('sync_statuses', function ($join) { $join->on('products.product_number', '=', 'sync_statuses.product_number')
DB::table('products')
->join('sync_statuses', function ($join) {
$join->on('products.product_number', '=', 'sync_statuses.product_number')
->where('products.updated_at', '>', 'sync_statuses.synced_at')
->whereNull('products.deleted_at');
})
->select('products.product_number');
此SQL表示我正试图通过以下方式实现:
SELECT products.product_number
FROM products
JOIN push_statuses
ON products.product_number = statuses.product_number
AND (
statuses.synced_at IS NULL
OR products.updated_at > statuses.synced_at
)
您必须使用on()
而不是where()
来比较列:
->on('products.updated_at', '>', 'sync_statuses.synced_at')
这对我很有用:
DB::table('products')
->join('statuses', function ($join) {
$join->on('products.product_number', '=', 'statuses.product_number')
->where(DB::raw('products.updated_at'), '>=', DB::raw('statuses.synced_at'))
->whereNull('products.deleted_at');
})->select('products.product_number');
我需要使用DB::raw('products.updated_at')
来引用where()子句中的每个日期时间列