我想为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")
答案 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
}