使用 Laravel 查询生成器的复杂 MySQL 内部联接查询

Complex MySQL inner join query with Laravel Query Builder

我正在尝试 运行 使用 Laravel 查询生成器进行此查询。 我认为查询是正确的,因为当我 运行 MySQL Workbench 中的查询执行时,我得到了预期的结果。 我知道我们可以使用 laravel 查询生成器编写原始查询,但它对 SQL 注入漏洞开放。所以我试图在没有原始查询的情况下继续前进。

这是查询

SELECT invoice.InvNo,customer.RouteCode,customer.CustomerCode,rootplan_product.RouteplanCode,invoice.Status 
FROM rootplan_product
INNER JOIN 
customer ON customer.RouteCode = rootplan_product.RouteCode
AND
customer.CustomerCode = rootplan_product.customercode
INNER JOIN
invoice ON invoice.CustomerCode = customer.CustomerCode 
WHERE
rootplan_product.RouteCode='MO-A' AND invoice.Status IN ('PENDING','ACTIVE') 
ORDER BY invoice.Status desc

我已经为每个 table 制作了一个模型,并像这样在控制器中使用。由于 table 名称与命名约定不同。我在每个模型中都添加了 protected $table = 'correct_table_name';

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use \App\Route;
use \App\Customer;
use \App\Invoice;
use \App\Rootplan_Product;

这是控制器函数

public function retrieveRouteCodeData(Request $request){
    try {
        $RouteCode = $request->RouteCode;
        $retrievedData = DB::table('rootplan_product')
                    ->join('customer', function($join){
                        $join->on('customer.RouteCode', '=', 'rootplan_product.RouteCode');
                        $join->on(DB::raw('(customer.CustomerCode = rootplan_product.CustomerCode)'));
                    })
                    ->join('invoice', 'invoice.CustomerCode', '=', 'customer.CustomerCode')
                    ->select('invoice.InvNo', 'customer.RouteCode', 'customer.CustomerCode', 'rootplan_product.RouteplanCode', 'invoice.Status')
                    ->where('rootplan_product.RouteCode', $RouteCode)
                    ->orderBy('invoice.Status','desc')
                    ->get();
                    return response()->json(['msg'=>'Updated Successfully', 'result'=>$retrievedData, 'success'=>true]);
      }
      catch (\Exception $e) {
          return response()->json(['msg'=>$e->getMessage()]);

      }


}

在控制台中我得到这个错误

"SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'on clause' (SQL: select invoice.InvNo, customer.RouteCode, customer.CustomerCode, rootplan_product.RouteplanCode, invoice.Status from rootplan_product inner join customer on customer.RouteCode = rootplan_product.RouteCode and (customer.CustomerCode = rootplan_product.CustomerCode) = `` inner join invoice on invoice.CustomerCode = customer.CustomerCode where rootplan_product.RouteCode = MO-A order by invoice.Status desc)"

我知道查询很复杂,如有任何帮助,我们将不胜感激!

问题出在 DB::raw 语句上。请使用以下内容:

DB::table('rootplan_product')->join('customer', function ($join) {
    $join->on('customer.RouteCode', '=', 'rootplan_product.RouteCode');
    $join->on('customer.CustomerCode', 'rootplan_product.CustomerCode');
})->join('invoice', 'invoice.CustomerCode', '=', 'customer.CustomerCode')
    ->select('invoice.InvNo', 'customer.RouteCode', 'customer.CustomerCode', 'rootplan_product.RouteplanCode', 'invoice.Status')
    ->where('rootplan_product.RouteCode', '123')
    ->whereIn('invoice.Status', ['PENDING','ACTIVE'])
    ->orderBy('invoice.Status', 'desc')
    ->get();

这将生成以下内容 SQL:

SELECT `invoice`.`InvNo`,
       `customer`.`RouteCode`,
       `customer`.`CustomerCode`,
       `rootplan_product`.`RouteplanCode`,
       `invoice`.`Status`
FROM `test`
INNER JOIN `customer` ON `customer`.`RouteCode` = `rootplan_product`.`RouteCode`
AND `customer`.`CustomerCode` = `rootplan_product`.`CustomerCode`
INNER JOIN `invoice` ON `invoice`.`CustomerCode` = `customer`.`CustomerCode`
WHERE `rootplan_product`.`RouteCode` = ?
  AND `invoice`.`Status` IN (?, ?)
ORDER BY `invoice`.`Status` DESC