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

时间:2018-12-19 12:15:22

标签: php mysql laravel laravel-query-builder laravel-5.7

我正在尝试使用Laravel查询生成器运行此查询。 我认为查询是正确的,因为当我在MySQL Workbench中运行查询时,查询将执行并得到预期的结果。我知道我们可以使用laravel查询生成器编写原始查询,但它对SQL开放注入漏洞。因此,我正在尝试不使用原始查询。

这是查询

audio/mp3

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

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;

在控制台中,我收到此错误

  

“ SQLSTATE [42S22]:找不到列:1054未知”在“ on子句”中(SQL:选择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()]); } } invoiceInvNocustomerRouteCodecustomerCustomerCoderootplan_productRouteplanCodeinvoice来自Status内部联接rootplan_product customercustomer = RouteCoderootplan_product和{customer.CustomerCode = rootplan_product.CustomerCode)=``RouteCode上的内部联接invoiceinvoice = CustomerCodecustomer,其中CustomerCoderootplan_product = MO-A订单,RouteCodeinvoice desc)“ < / p>

我了解该查询很复杂,我们将不胜感激!

1 个答案:

答案 0 :(得分:3)

问题出在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