Laravel Eloquent Subquery在带别名的连接中

时间:2015-06-08 07:16:11

标签: sql laravel

我喜欢从第一个表中选择所有信息,并计算第二个表中特定列的数据以及它们之间的关系。

这是SQL查询

Select * from emp_informations e inner
join(Select SUM(latein) as totallate, enrollnum from dtrs where latein > 0 
and (date_in between 2015-03-01 and 2015-03-31) and (date_out between 2015-
03-01 and 2015-03-31)  group by enrollnum)dtrs on dtrs.enrollnum=e.EmpID 
group by EmpID 

这个查询的laravel代码是什么?

$employees=DB::select(DB::raw('Select * from emp_informations e join (Select 
SUM(latein) as totallate, enrollnum from dtrs where latein > 0 and date_in 
between 2015-03-01 and 2015-03-31 and date_out between 2015-03-01 and 2015-
03-31  group by enrollnum)dtrs on dtrs.enrollnum=e.EmpID group by EmpID '));

但没有显示..

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题,这是代码..

$employees = DB::table('emp_informations')
    ->join(DB::raw("(Select SUM(latein) as totallate, enrollnum from dtrs 
    where latein > 0 and (date_in between '2015-03-01' and '2015-03-31' and 
    date_out between '2015-03-01' and '2015-03-31') group by 
    enrollnum)dtrs"), function($join)
    {
        $join->on('emp_informations.EmpID', '=', 'dtrs.enrollnum');
    })
    ->groupBy('EmpID')
    ->get();