问题描述
我不想相信laravel 5.5 cos中有一个错误,当我在查询中使用groupBy时,我如何惊讶地在Eloquent Query Builder上出现错误.
I dont want to believe there is a bug in laravel 5.5 cos i am amazed how i keep getting errors on the Eloquent Query Builder when ever i use the groupBy in my query.
试图将此无缝运行的SQL转换为Eloquent,并且我在groupBy上不断收到错误消息.
Tried converting this SQL that works seamlessly to Eloquent and i keep getting Errors on the groupBy..
我在这里抱怨过一个类似的SQL问题,直到现在我还没有一个完美的答案此处未解决问题
Had a similar issue with an SQL which i complained here and till now i have not gotten a perfect answer for itopen issue here
SELECT * FROM arm_articles
INNER JOIN arm_interest ON arm_articles.article_category = arm_interest.category_id
WHERE arm_articles.article_contributor_id='1322'
GROUP BY arm_articles.article_id
返回错误的电子版本
$contributor_id =1322;
DB::table('arm_articles')
->join('arm_interest', function($join) {
$join->on('arm_articles.article_category','=','arm_interest.category_id');
})
->having('arm_articles.article_contributor_id', '=', $contributor_id)
->groupBy('arm_articles.article_id')->get()
错误消息
SQLSTATE [42000]:语法错误或访问冲突:1055'knowledge_db.arm_articles.id'不在GROUP BY中(SQL:从'arm_articles'内部联接'arm_articles'的内部联接'arm_interest'中选择*.'article_category'='arm_interest'.'category_id'由'arm_articles'.'article_id'具有'arm_articles'.'article_contributor_id'= 1322)分组
因此,就像我说的b4一样,为什么我会因为首先向查询中添加-> toSql
而返回正确的SQL的原因而感到头疼.
So like i said b4 its really weared why i will get the error in the first place as adding ->toSql
to the query returns the right SQL.
推荐答案
-
将此配置添加到
config/database.php
中的mysql数据库配置中:Add this configuration to your mysql database config in
config/database.php
:'mysql' => [ .... 'strict' => false, //'strict' => true, ],
-
在您的mysql配置文件(
my.ini
或my.cnf
)中编辑sql_mode
[mysqld] Edit
sql_mode
in your mysql config file (my.ini
ormy.cnf
)[mysqld]sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
-
重新启动MySQL
Restart your MySQL
这篇关于Laravel Eloquent Builder中的GroupBy错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!