當我手動運行下面的SQL時,我得到的預期結果沒有錯誤select * from `crawl_results` where `user_id` = 1 and `website_id` = 1 and `item_state` != 'OK' group by `destination_url` limit 30 offset 0但是,當我在Eloquent中運行此命令時...self::where('user_id', Auth::id()) ->where('website_id', $scanID) ->where('item_state', '!=' , 'OK') ->groupby('destination_url')->paginate(30)它產生此錯誤:SQLSTATE [42000]:語法錯誤或訪問沖突:1055'link_checker.crawl_results.id'不在GROUP BY中(SQL:選擇* from crawl_resultswhere user_id= 1 and website_id= 1 and item_state!= OK group by destination_urllimit 30 offset 0)不確定產生該錯誤的抽象背后發生了什么?
2 回答

SMILET
TA貢獻1796條經驗 獲得超4個贊
將查詢更改為查詢構建器查詢
DB::table('crawl_results')->where('user_id', Auth::id())->where('website_id', $scanID)->where('item_state', '!=' , 'OK')->groupby('destination_url')->paginate(30);
它應該工作正常。
在文檔中提到了GROUP BY無法在分頁中有效執行
當前,使用Laravel無法有效地執行使用groupBy語句的分頁操作。如果需要使用帶有分頁結果集的groupBy,建議您查詢數據庫并手動創建分頁器。

斯蒂芬大帝
TA貢獻1827條經驗 獲得超8個贊
您應該轉到 config \ database.php并將strict更改為false
'mysql' => [
...
'strict' => false,
...
]
- 2 回答
- 0 關注
- 261 瀏覽
添加回答
舉報
0/150
提交
取消