Sql 如何将laravel中三个不同表格中的三列相加

Sql 如何将laravel中三个不同表格中的三列相加,sql,laravel,Sql,Laravel,我需要在一个查询中包含所有这三个查询。请帮我考虑一下。我对拉威尔很陌生 $month = date('m'); $breakfast_cost=DB::table('breakfast_orders') ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date') ->where('breakfast_orders.user_id',$id) -&g

我需要在一个查询中包含所有这三个查询。请帮我考虑一下。我对拉威尔很陌生

    $month = date('m'); 

    $breakfast_cost=DB::table('breakfast_orders')
    ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
    ->where('breakfast_orders.user_id',$id)
    ->whereMonth('breakfast_orders.created_at',$month)
    ->sum('breakfast_costs.individual_cost');

    $lunch_cost=DB::table('lunch_orders')
    ->join('lunch_costs','lunch_orders.date','=','lunch_costs.date')
    ->where('lunch_orders.user_id',$id)
    ->whereMonth('lunch_orders.created_at',$month)
    ->sum('lunch_costs.individual_cost');

    $dinner_cost=DB::table('dinner_orders')
    ->join('dinner_costs','dinner_orders.date','=','dinner_costs.date')
    ->where('dinner_orders.user_id',$id)
    ->whereMonth('dinner_orders.created_at',$month)
    ->sum('dinner_costs.individual_cost');

    $total_cost=($breakfast_cost+$lunch_cost+$dinner_cost);

如果对您有帮助,请尝试此功能

$total_cost = DB::table('users')
    ->selectRaw('SUM(breakfast_costs.individual_cost) as breakfast_sum, SUM(lunch_costs.individual_cost) as lunch_costs_sum, SUM(dinner_costs.individual_cost) as dinner_costs_sum')
    ->join('breakfast_orders','breakfast_orders.user_id','=','users.id')
    ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
    ->join('lunch_orders','lunch_orders.user_id','=','users.id')
    ->join('lunch_costs','lunch_orders.date','=','lunch_costs.date')
    ->join('dinner_orders','dinner_orders.user_id','=','users.id')
    ->join('dinner_costs','dinner_orders.date','=','dinner_costs.date')
    ->whereMonth('breakfast_orders.created_at',$month)
    ->whereMonth('lunch_orders.created_at',$month)
    ->whereMonth('dinner_orders.created_at',$month)
    ->where('users.id',$id)
    ->first();
return $total_cost;

还请注意,如果您有这些表中的任何一个是可选的,比如它可能有用户的数据,也可能没有,那么您需要使用
leftJoin
righjoin
而不是
join
,否则如果其中任何一个表失败,它将不会返回准确的记录如果有帮助,请尝试此操作

$total_cost = DB::table('users')
    ->selectRaw('SUM(breakfast_costs.individual_cost) as breakfast_sum, SUM(lunch_costs.individual_cost) as lunch_costs_sum, SUM(dinner_costs.individual_cost) as dinner_costs_sum')
    ->join('breakfast_orders','breakfast_orders.user_id','=','users.id')
    ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
    ->join('lunch_orders','lunch_orders.user_id','=','users.id')
    ->join('lunch_costs','lunch_orders.date','=','lunch_costs.date')
    ->join('dinner_orders','dinner_orders.user_id','=','users.id')
    ->join('dinner_costs','dinner_orders.date','=','dinner_costs.date')
    ->whereMonth('breakfast_orders.created_at',$month)
    ->whereMonth('lunch_orders.created_at',$month)
    ->whereMonth('dinner_orders.created_at',$month)
    ->where('users.id',$id)
    ->first();
return $total_cost;

另外请注意,如果您有这些表中的任何一个是可选的,比如它可能有用户的数据,也可能没有,那么您需要使用
leftJoin
righjoin
而不是
join
,否则,如果其中任何一个表失败,它将不会返回准确的记录

这三个查询之间是否有关系?是的,同一用户可以为特定日期订购早餐、午餐和晚餐。没有创建任何模型。尝试使用查询生成器这三个查询中是否存在任何关系?是的,同一用户在特定日期有早餐、午餐和晚餐的订单。没有创建任何模型。试图处理查询builderit成功了,只缺少了第一个括号。但结果是错误的。你的查询显示了所有订单的总和,而不是个人订单是的,这很简单,不是一个问题检查,更新了个人总数的答案,你知道这是怎么回事,它只是汇总订单,汇总午餐,汇总早餐,然后将这三项加在一起。兄弟,我希望能就zoom进行对话,但现在我在办公室,你可以理解。但你可以为你的问题写一个详细的场景。它成功了,只缺少了第一个括号。但结果是错误的。你的查询显示了所有订单的总和,而不是个人订单是的,这很简单,不是一个问题检查,更新了个人总数的答案,你知道这是怎么回事,它只是汇总订单,汇总午餐,汇总早餐,然后将这三项加在一起。兄弟,我希望能就zoom进行对话,但现在我在办公室,你可以理解。但是你可以为你的问题写一个详细的场景。