如何排序具有关系的记录在查询结果中排在最后
How to order records with having a relationship comes last in query result
我有两个表,名为 calls
和 call_histories
。例如下面是我的查询。
$calls = Call::select([
'calls.id as id', 'calls.reserved_at', 'calls.app_id',
'calls.call_type_id', 'call_histories.call_id'
])->leftJoin('call_histories', 'calls.id', '=', 'call_histories.call_id')
->whereIn('call_type_id', $callTypesIds)
->where(DB::raw("(DATE_FORMAT(reserved_at,'%Y-%m-%d'))"), '<=',
Carbon::now()->format('Y-m-d'))
->where('app_id', $callAppId)
->free()
->orderBy('reserved_at')
->take(100);
我想知道一个电话是否有任何call_histories
订单作为查询结果中的最后记录。我该怎么做?
因为如果找不到 call_histories.call_id
将为 null,您可以按此排序:
$calls = Call::select([
'calls.id as id', 'calls.reserved_at', 'calls.app_id', 'calls.call_type_id', 'call_histories.call_id'
])->leftJoin('call_histories', 'calls.id', '=', 'call_histories.call_id')
->whereIn('call_type_id', $callTypesIds)
->where(DB::raw("(DATE_FORMAT(reserved_at,'%Y-%m-%d'))"), '<=', Carbon::now()->format('Y-m-d'))
->where('app_id', $callAppId)
->free()
->orderBy('reserved_at')
->orderBy('call_id')
->take(100);
我有两个表,名为 calls
和 call_histories
。例如下面是我的查询。
$calls = Call::select([
'calls.id as id', 'calls.reserved_at', 'calls.app_id',
'calls.call_type_id', 'call_histories.call_id'
])->leftJoin('call_histories', 'calls.id', '=', 'call_histories.call_id')
->whereIn('call_type_id', $callTypesIds)
->where(DB::raw("(DATE_FORMAT(reserved_at,'%Y-%m-%d'))"), '<=',
Carbon::now()->format('Y-m-d'))
->where('app_id', $callAppId)
->free()
->orderBy('reserved_at')
->take(100);
我想知道一个电话是否有任何call_histories
订单作为查询结果中的最后记录。我该怎么做?
因为如果找不到 call_histories.call_id
将为 null,您可以按此排序:
$calls = Call::select([
'calls.id as id', 'calls.reserved_at', 'calls.app_id', 'calls.call_type_id', 'call_histories.call_id'
])->leftJoin('call_histories', 'calls.id', '=', 'call_histories.call_id')
->whereIn('call_type_id', $callTypesIds)
->where(DB::raw("(DATE_FORMAT(reserved_at,'%Y-%m-%d'))"), '<=', Carbon::now()->format('Y-m-d'))
->where('app_id', $callAppId)
->free()
->orderBy('reserved_at')
->orderBy('call_id')
->take(100);