简体   繁体   中英

Converting mysql query to Eloquent query builder

So I am having trouble with this writting this mysql query in Laravel Eloquent query builder. This is mysql query.

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 is what I have so far, but its not working as I get a error that I have an error in my syntax.

$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')

any ideas where I am going wrong?

I see a syntax error on left join ( o,location => o.location ), but anyway try this:

$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);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM