将mysql查询转换为口才查询生成器

时间:2020-10-29 20:24:19

标签: php mysql laravel eloquent

所以我在用Laravel Eloquent查询构建器编写这个mysql查询时遇到了麻烦。这是mysql查询。

SELECT a.Location, a.AptDate, a.AptTime, IF(a.AptStatus = 3, 500, AptLength) AptLength,
a.AptStatus, o.opname, a.OperatoryNum
FROM schedules a
LEFT JOIN ops o ON a.Location = o.location AND (a.Operatory = o.opname OR a.AptStatus = 3)

到目前为止,Thi是我所拥有的,但是由于出现语法错误而出现错误,因此无法正常工作。

$arrAppointment = DB::table('schedules a')
                                ->select("a.Location, a.AptDate, a.AptTime",
                                    DB::raw('IF(a.AptStatus = 3, 500, AptLength)'), 'AptLength,a.AptStatus, o.opname, a.OperatoryNum' )
                                ->leftJoin('ops o', 'a.Location = o,location AND a.Operatory = o.opname or a.AptStatus = 3')

有什么想法我要去哪里吗?

1 个答案:

答案 0 :(得分:0)

我在左连接(o,location => o.location)上看到语法错误,但是无论如何都尝试这样做:

$arrAppointment = DB::table('schedules a')
->selectRaw('a.Location, a.AptDate, a.AptTime, IF(a.AptStatus = 3, 500, AptLength) AptLength,a.AptStatus, o.opname, a.OperatoryNum' )
->leftJoin(DB::raw('ops as o'), function($join) {
    $join->on('a.Location', '=', 'o.location');
    $join->where(function($q) {
        $q->where('a.Operatory','=','o.opname');
        $q->orWhere('a.AptStatus','=', 3);
    });
});
相关问题