请教我如何laravel-querybuilder left join

时间:2015-07-24 07:17:02

标签: laravel laravel-4 laravel-5

select t2.id,name,count(*) as toroku ,
sum(case when tion_flg <> 0 THEN 1     ELSE 0 END ) as riyou,t2.upload_at
FROM id_master as t1
LEFT JOIN id_upload as t2
ON t1.upload_id = t2.upload_id
GROUP BY t2.id,t2.name,t2.upload_at
ORDER BY id

我&#39;我喜欢做laravel查询。

1 个答案:

答案 0 :(得分:0)

试试这个

DB::table('id_master AS t1')
    ->select(DB::raw('
        t2.id,
        name,
        count(*) AS toroku,
        SUM(CASE WHEN tion_flg <> 0 THEN 1 ELSE 0 END ) AS riyou,
        t2.upload_at'))
    ->leftJoin('id_upload AS t2', 't1.upload_id', '=', 't2.upload_id')
    ->groupBy('t2.id','t2.name','t2.upload_at')
    ->orderBy('id')
->get();