gpt4 book ai didi

php - 使用 Laravel Query Builder 进行复杂的 MySQL 内连接查询

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:42 25 4
gpt4 key购买 nike

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

这是查询

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

我已经将每个表都制作成一个模型,并像这样在 Controller 中使用。由于表名与命名约定不同。我在每个模型中都添加了 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;

这是 Controller 函数

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

关于php - 使用 Laravel Query Builder 进行复杂的 MySQL 内连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53851077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com