laravel 子查询

select 子查询 

->addSelect(['method_time' => function ($query) {
    $query->select('created_at')
        ->from('customer_method')
        ->whereColumn('method_id', 'apply.id')
        ->where('customer_method.table','apply')
        ->where('customer_method.follow_method',59)
        ->orderBy('created_at', 'desc')
        ->limit(1);
}])

orderby 子查询

return User::orderBy(function ($query) {
    $query->select('created_at')
        ->from('logins')
        ->whereColumn('user_id', 'users.id')
        ->latest()
        ->limit(1);
})->get();

from 子查询

return DB::table(function ($query) {
    $query->selectRaw('sum(amount) as total')
        ->from('donations')
        ->groupBy('user_id');
}, 'donations')->avg('total');

你可能感兴趣的:(laravel,mysql,php,数据库,sql)