gpt4 book ai didi

php - 如何在 leftjoin 中使用多个条件与 laravel5 中的原始查询

转载 作者:行者123 更新时间:2023-11-29 03:20:57 25 4
gpt4 key购买 nike

我需要连接两个表以在 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"))

最佳答案

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();

关于php - 如何在 leftjoin 中使用多个条件与 laravel5 中的原始查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com