Laravel对用户模型的原始查询

时间:2017-03-24 14:43:57

标签: mysql laravel eloquent laravel-eloquent

我有一个原始的SQL查询,我想从中生成Laravel Eloquent模型。

查询:

SELECT * FROM user
LEFT JOIN (SELECT user_id, MAX(log_login_id) AS maxid
FROM log_login
GROUP BY user_id) 
AS T1 ON user.user_id = T1.user_id
LEFT JOIN log_login AS T2 ON T2.log_login_id = T1.maxid
WHERE user.map_id = 101;

我正在尝试这样的事情,但我的工作并不好......

 $users = User::where('user.map_id', session('mapID'))
        ->leftJoin('(SELECT user_id, MAX(log_login_id) AS maxid
                   FROM log_login
                   GROUP BY user_id) 
                   AS T1 ON user.user_id = T1.user_id')
        ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
        ->orderBy('username', 'asc')->get();

如何设置我的查询,以便从Eloquent获取对象模型列表?这是一个语法问题,我想。

错误:

QueryException in Connection.php line 624:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `u' at line 1 (SQL: select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = 101 order by `username` asc)

in Connection.php line 624
at Connection->runQueryCallback('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), object(Closure)) in Connection.php line 580
at Connection->run('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), object(Closure)) in Connection.php line 297
at Connection->select('select * from `user` left join `(SELECT` as `MAX(log_login_id)` on `` `` left join `log_login` as `T2` on `` `` where `user`.`map_id` = ? order by `username` asc', array('101'), true) in Builder.php line 1377
at Builder->runSelect() in Builder.php line 1367
at Builder->getFresh(array('*')) in Builder.php line 1354
at Builder->get(array('*')) in Builder.php line 380

经过多次尝试和错误后,这就成功了:

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(DB::raw('(SELECT user_id,  MAX(log_login_id) AS maxid
       FROM log_login
       GROUP BY user_id) 
       AS T1'), function ($join) {
        $join->on("user.user_id", "=", "T1.user_id");
    })
    ->leftJoin(DB::raw('log_login AS T2'), function ($join) {
        $join->on('T2.log_login_id', '=', 'T1.maxid');
    })->get();

1 个答案:

答案 0 :(得分:2)

试试这个

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(\DB::raw('(SELECT user_id, MAX(log_login_id) AS maxid
               FROM log_login
               GROUP BY user_id) 
               AS T1 ON user.user_id = T1.user_id'))
    ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
    ->orderBy('username', 'asc')->get();

或者这个(没有经过测试但是猜它应该有用)

$users = User::where('user.map_id', session('mapID'))
    ->leftJoin(function($query){
       $query->select("user_id", "MAX(log_login_id) as maxid")
           ->from('log_login as T1')
           ->groupBy('user_id')
           ->on('user.user_id','=','T1.user_id')
    })
    ->leftJoin('log_login AS T2 ON T2.log_login_id = T1.maxid')
    ->orderBy('username', 'asc')->get();

希望有所帮助