【问题标题】:How to use multiple condition in leftjoin with raw query in laravel5如何在laravel5中的原始查询中使用leftjoin中的多个条件
【发布时间】:2017-07-21 06:25:34
【问题描述】:

我需要连接两个表来获取leftjoin上具有多个条件的数据,但是
我收到这个错误。 on 子句的参数不足 我使用的是 laravel5.2。如何将原始查询与 leftjoin 多个条件一起使用。

    $activityDetail = DB::table('table1 as UA')
        ->selectRaw('SUM(UA.total_calory) as total_calory,
            SUM(UA.flight_descend) as flight_descend,date('.$tz_start_date.') as start_date')
        ->leftjoin('table2 as LC',function($join) use($tz_lccreated_date,$dateRange){
                $join->on('LC.user_id_fk','=','UA.user_id_fk');
                $join->on('LC.is_active','=',DB::raw('1'));
                $join->on(' date('.DB::raw($tz_lccreated_date).') '.DB::raw($dateRange));
            })
            ->whereRaw(' date('.$tz_start_date.') '.$dateRange)
            ->where('UA.user_id_fk','=',base64_decode($params['uid']))
            ->groupBy(DB::raw('date('.$tz_start_date.')'))
            ->get();

Raw query is 

select SUM(UA.total_calory) as total_calory,
SUM(UA.flight_descend) as flight_descend,
date(CONVERT_TZ(UA.start_date,"+00:00","+05:30")) as start_date 
from `table1` as `UA` 
left join `table2` as `LC` 
on `LC`.`user_id_fk` = `UA`.`user_id_fk` and `LC`.`is_active` = 1
and `date(CONVERT_TZ(LC`.`created_date,"+00:00","+05:30"))` = `current_date` 
where date(CONVERT_TZ(UA.start_date,"+00:00","+05:30")) = current_date
and `UA`.`user_id_fk` = 411 
group by date(CONVERT_TZ(UA.start_date,"+00:00","+05:30"))

【问题讨论】:

  • 我已经解决了我的问题,经过大量分析,我没有在 join 子句中得到任何原始查询的解决方案,然后我正在使用子查询并解决我的问题。

标签: php mysql laravel-5.2


【解决方案1】:
I have resolved my problem, after a lots of analysis, i am not getting any kind of solution of raw  query in join clause then i am using subquery and resolve my problem.

$mannualLoggedQuery = 'IFNULL((select sum(calory) as cal from table2 as LC where LC.user_id_fk = "'.base64_decode($params['uid']).'" and date('.$tz_lccreated_date.')'.$dateRange.'),0)';


$activityDetail = DB::table('table1 as UA')->selectRaw('SUM(UA.total_calory) + '.$mannualLoggedQuery.' as total_calory,

                                    date('.$tz_start_date.') as start_date')
                                    ->where('UA.user_id_fk','=',base64_decode($params['uid']))
                                    ->whereRaw('date('.$tz_start_date.') '.$dateRange)
                                    ->groupBy(DB::raw('date('.$tz_start_date.')'))
                                    ->get();

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2015-07-10
    • 2014-11-21
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    相关资源
    最近更新 更多