Laravel 5.2:如何在此右连接中编写带2 ON的查询构建器

时间:2016-11-05 12:45:34

标签: laravel

我有这个sql语法

    SELECT vehicles.license_plate, vehicles.year, vehicles.status, vehicles.zone_id, vehicles.car_class_id, harga_sewa.value, harga_sewa.description
FROM  `vehicles` 
RIGHT JOIN  `harga_sewa` ON vehicles.zone_id = harga_sewa.zone_id
AND vehicles.car_class_id = harga_sewa.car_class_id

我怎么写这样的东西?

DB::table('vehicles')
                ->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id', AND vehicles.car_class_id = harga_sewa.car_class_id)
                ->select(*)

2 个答案:

答案 0 :(得分:3)

尝试以下代码

DB::table('vehicles')
            ->rightJoin('harga_sewa', function ($join) {
                $join->on('vehicles.zone_id', '=', 'harga_sewa.zone_id');
                $join->on('vehicles.car_class_id', '=', 'harga_sewa.car_class_id');
            })
            ->get();

答案 1 :(得分:0)

在移动设备上,所以无法测试,但是从内存开始这应该可行:

DB::table('vehicles')
  ->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id')
  ->rightJoin('harga_sewa', 'vehicles.car_class_id', '=', 'harga_sewa.car_class_id')
  ->select('*')
  ->get();