我有以下查询:
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子句的任何工作示例。
答案 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了解详情