在Laravel查询Bulider中使用和子句

时间:2017-10-04 18:33:05

标签: php laravel laravel-5 laravel-5.3 laravel-query-builder

我有以下查询:

    SELECT
  customers.CustCompanyName,
  addresses.AddressLine1,
  orders.*
FROM orders
  INNER JOIN customers
    ON orders.CustID = customers.CustID
  INNER JOIN customer_addresses
    ON customer_addresses.CustID = customers.CustID
  INNER JOIN addresses
    ON customer_addresses.AddressID = addresses.AddressID
    AND orders.CustAddID = customer_addresses.CustAd

如何在使用laravel查询生成器时合并AND子句:

$orders = DB::table('orders')
        ->join('customers', 'orders.CustID', '=', 'customers.CustID')
        ->join('customer_addresses','customer_addresses.CustID', '=' , 'customers.CustID' )
        ->join('addresses','customer_addresses.AddressID','=','addresses.AddressID')->AND

我找不到使用AND子句的任何工作示例。

1 个答案:

答案 0 :(得分:0)

有关:

INNER JOIN addresses
   ON customer_addresses.AddressID = addresses.AddressID
   AND orders.CustAddID = customer_addresses.CustAd

你应该使用:

->join('addresses', function($join) {
   $join->on('customer_addresses.AddressID', '=', 'addresses.AddressID')
   ->whereRaw('orders.CustAddID = customer_addresses.CustAd');
});

您还可以查看Joins in Query Builder了解详情