左联接未按预期工作,但SQL接受了它

时间:2019-03-25 09:48:23

标签: php laravel laravel-query-builder

HomeController.php

public function index()
{      
    $employees = DB::table('timings')
                ->distinct()
                ->selectRaw("timings.employee_id, min(time_logged) AS minTime, max(time_logged) AS maxTime")
                ->leftJoin('employees','timings.employee_id','=','employees.employee_id')
                ->where( DB::raw('cast(time_logged AS date)') , DB::raw('CURDATE()'))
                ->groupBy('timings.employee_id', DB::raw('cast(time_logged AS date)') )
                ->get();

    return view('home',compact('employees'));
}

实际SQL

employee_id  |   minTime     |    maxTime     
1                2019-03-25       09:00:00 2019-03-25 14:00:00
...

结果

Array ( 
    [0] => stdClass Object ( 
    [employee_id] => 1 
    [minTime] => 2019-03-25 09:00:00 
    [maxTime] => 2019-03-25 14:00:00 
)
... 

由于某种原因,MYSQL接受左联接,但什么也没有发生!我有2张桌子,员工和时间安排。无论我包括leftJoin还是将其注释掉,代码都可以工作,但EMPLOYEE表不会显示。我不知道为什么它不起作用。

1 个答案:

答案 0 :(得分:0)

您没有从employee表中选择详细信息

->selectRaw("timings.employee_id, min(time_logged) AS minTime, max(time_logged) AS maxTime,employees.first_name, employees.last_name")