一尘不染

使用DB :: raw和Eloquent使用同一查询的不同结果

sql

运行Eloquent联接查询时,我得到了一些意外的结果。通过使用完全相同的查询,我得到两个不同的结果。一个使用DB ::
raw()运行,第二个使用Eloquent。

在口才查询中,与

where squad_user.leave_time >= seasons.start_time

丢失,将不会包含在结果集中。符合条件的用户

or squad_user.leave is null

但是将包括在内。

这是两个查询结果的唯一区别。原始查询实际上会产生所需的结果集。

什么 真正 困扰我的是,如果我检查查询日志,既Laravel的和MySQL,我同时运行原料和雄辩的查询时,得到完全相同的查询。

原始查询 (运行口才查询时,我从查询日志中获得的 实际 查询)

return \DB::select(\DB::raw('
        select users.*
        from users
        inner join squad_user on users.id = squad_user.user_id
        inner join seasons on squad_user.squad_id = seasons.squad_id
        where squad_user.join_time <= seasons.end_time
        and (squad_user.leave_time >= seasons.start_time or squad_user.leave_time is null)
        and seasons.id = :seasonId
    '),
    ['seasonId' => 3]
);

口才查询

return User::join('squad_user', 'users.id', '=', 'squad_user.user_id')
           ->join('seasons', 'squad_user.squad_id', '=', 'seasons.squad_id')
           ->where('squad_user.join_time', '<=', 'seasons.end_time')
           ->where(function ($query)
           {
               $query->where('squad_user.leave_time', '>=', 'seasons.start_time')
                     ->orWhereNull('squad_user.leave_time');
           })
           ->where('seasons.id', 3)
           ->get(['users.*']);

Laravel雄辩的查询日志

select `users`.*
from `users`
inner join `squad_user` on `users`.`id` = `squad_user`.`user_id`
inner join `seasons` on `squad_user`.`squad_id` = `seasons`.`squad_id`
where `squad_user`.`join_time` <= seasons.end_time
and (`squad_user`.`leave_time` >= seasons.start_time or `squad_user`.`leave_time` is null)
and `seasons`.`id` = 3
{"bindings":["seasons.end_time","seasons.start_time",3],"time":0.38,"name":"mysql"}

MySQL的Eloquent查询的general_log

select `users`.*
from `users`
inner join `squad_user` on `users`.`id` = `squad_user`.`user_id`
inner join `seasons` on `squad_user`.`squad_id` = `seasons`.`squad_id`
where `squad_user`.`join_time` <= ?
and (`squad_user`.`leave_time` >= ? or `squad_user`.`leave_time` is null)
and `seasons`.`id` = ?

MySQL在raw查询上的general_log

select users.*
            from users
            inner join squad_user on users.id = squad_user.user_id
            inner join seasons on squad_user.squad_id = seasons.squad_id
            where squad_user.join_time <= seasons.end_time
            and (squad_user.leave_time >= seasons.start_time or squad_user.leave_time is null)
            and seasons.id = ?

我会很感激这里的任何指示,因为我很失落。


阅读 201

收藏
2021-05-23

共1个答案

一尘不染

where绑定第三参数并将其通常视为字符串,除非您通过使用raw语句告诉它不这样做。DB::rawwhereRaw将为您工作:

return User::join('squad_user', 'users.id', '=', 'squad_user.user_id')
       ->join('seasons', 'squad_user.squad_id', '=', 'seasons.squad_id')
       ->where('squad_user.join_time', '<=', DB::raw('seasons.end_time'))
       ->where(function ($query)
       {
           $query->where('squad_user.leave_time', '>=', DB::raw('seasons.start_time'))
                 ->orWhereNull('squad_user.leave_time');
       })
       ->where('seasons.id', 3)
       ->get(['users.*']);
2021-05-23