Mysql
 sql >> Database >  >> RDS >> Mysql

subquery con join in laravel

prova a inviare la sottoquery come espressione grezza come primo parametro in leftJoin() metodo, ad esempio:

$subquery = '(SELECT 
                    usr_log1.*
                FROM
                    users_log_logradouro AS usr_log1
                LEFT JOIN 
                    users_log_logradouro AS usr_log2 
                    ON usr_log1.user_id = usr_log2.user_id
                    AND usr_log1.created_at < usr_log2.created_at
                WHERE
                    usr_log2.user_id IS NULL) 
                AS temp';

    DB::table('users as usr')
        ->select(...)
        ->leftJoin(DB::raw($subquery), 'usr.id', '=', 'temp.user_id')
        ->leftJoin(...)
        ->join(...)
        ->join(...)
        ->join(...)
        ->leftJoin(...)
        ->leftJoin(...)
        ->leftJoin(...)
        ->where(...)
        ->get()

ci sono esempi simili in queste domande:

Laravel Fluent Query Builder Unisciti con la sottoquery

Come scrivere this (left join, subquery ) in Laravel 5.1?

maggiori informazioni abaut si unisce:https://laravel.com/docs/5.5/queries#joins