Laravel-使用DB :: raw()查询的联接中的其他条件

时间:2019-06-06 11:55:56

标签: laravel

我想为DB::raw查询join添加一个“或”条件

我有这个查询:

$billings = DB::table("billings")
    ->select("billings.plan", "billings.email",DB::raw("COUNT(billings.plan) as total_plans") ,DB::raw("SUM(billings.amount) as total_amount"))
    ->join("users","users.email","=","billings.email")
    ->groupBy("billings.plan")
    ->orderByRaw('billings.plan ASC');

我有:

->join("users","users.email","=","billings.email")

我要使用“或”条件:

->join("users","users.email","=","billings.email")

->join("users","users.phone","=","billings.phone")

3 个答案:

答案 0 :(得分:0)

尝试以下:

$billings = DB::table("billings")
    ->select("billings.plan", "billings.email", DB::raw("COUNT(billings.plan) as total_plans"), DB::raw("SUM(billings.amount) as total_amount"))
    ->join("users", function ($join) {
        $join->on("users.email", "=", "billings.email")
            ->orOn("users.phone", "=", "billings.phone")
    })
    ->groupBy("billings.plan")
    ->orderByRaw('billings.plan ASC');

答案 1 :(得分:0)

$billings = DB::table("billings")
           ->select("billings.plan", "billings.email",DB::raw("COUNT(billings.plan) as total_plans") ,DB::raw("SUM(billings.amount) as total_amount"))
            ->join("users",function($join){
                  $join->on("users.email","=","billings.email");
                  $join->orOn("users.phone","=","billings.phone");
             })
            ->groupBy("billings.plan")
            ->orderByRaw('billings.plan ASC');

答案 2 :(得分:0)

为此,您需要使用高级连接子句。 像这样:-

{
  "id" => 123,
  "position" => NaN
}