我正在使用 laravel 8,并且我制作了一個查詢生成器來進行左連接,當 laravel 執行查詢時顯示錯誤,但在 SQL Server MS 中正確執行
SQLSTATE[42S22]:[Microsoft][用于 SQL Server 的 ODBC 驅動程式 17][SQL Server]無效的列名“2”。(SQL: select [a].[id] as [id1], [b].[id] as [id2], [a].[reg], [a].[parte], [a].[ubiclinea ], [a].[descripcion], [a].[numConteo] 作為 [conteo1], [a].[cantidad] 作為 [cantidad1], [a].[counter] 作為 [contador1], [a]。 [created_at] 作為 [created1],[b].[numConteo] 作為 [conteo2],[b].[cantidad] 作為 [cantidad2],[b].[counter] 作為 [contador2],[b].[created_at] ] as [created2] from [directosCount] as [a] left join [directosCount] as [b] on [a].[reg] = [b].[reg] and [b].[numConteo] = [2] [b].[deleted_at] 為空,其中 [a].[numConteo] = 1 且 [a].[deleted_at] 為空)
那是顯示的錯誤
但我復制查詢并在 SQL Server 中執行,它作業正常
我在 Laravel 中的查詢是這樣的
DB::table('directosCount as a')
->leftJoin('directosCount as b', function($join)
{
$join->on('a.reg', '=', 'b.reg');
$join->on('b.numConteo', '=', '2')->whereNull('b.deleted_at');
})
->select('a.id as id1', 'b.id as id2', 'a.reg', 'a.parte', 'a.ubiclinea', 'a.descripcion', 'a.numConteo as conteo1', 'a.cantidad as cantidad1', 'a.counter as contador1', 'a.created_at as created1', 'b.numConteo as conteo2', 'b.cantidad as cantidad2', 'b.counter as contador2', 'b.created_at as created2')
->where('a.numConteo', '1')
->whereNull('a.deleted_at')
->get();
希望可以有人幫幫我
uj5u.com熱心網友回復:
我認為這應該在 join 之外
$join->on('b.numConteo', '=', '2')->whereNull('b.deleted_at');
請on改為where
->where('b.numConteo', '=', '2')->whereNull('b.deleted_at');
uj5u.com熱心網友回復:
您的聯接中有 2 條on陳述句。試試這個
DB::table('directosCount as a')
->leftJoin('directosCount as b', function($join)
{
$join->on('a.reg', '=', 'b.reg')
->where('b.numConteo', '=', '2')
->whereNull('b.deleted_at');
})
->select('a.id as id1', 'b.id as id2', 'a.reg', 'a.parte', 'a.ubiclinea', 'a.descripcion', 'a.numConteo as conteo1', 'a.cantidad as cantidad1', 'a.counter as contador1', 'a.created_at as created1', 'b.numConteo as conteo2', 'b.cantidad as cantidad2', 'b.counter as contador2', 'b.created_at as created2')
->where('a.numConteo', '1')
->whereNull('a.deleted_at')
->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/384615.html
標籤:sql-server 拉拉维尔
