doujiyan0971 2018-02-14 14:30
浏览 105

Laravel Eloquent Builder中的GroupBy错误

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.

Tried converting this SQL that works seamlessly to Eloquent and i keep getting Errors on the groupBy..

Had a similar issue with an SQL which i complained here and till now i have not gotten a perfect answer for it open 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

ELOQUENT VERSION THAT RETURNS ERROR

$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()

ERROR MESSAGE

SQLSTATE[42000]: Syntax error or access violation: 1055 'knowledge_db.arm_articles.id' isn't in GROUP BY (SQL: select * from 'arm_articles' inner join 'arm_interest' on 'arm_articles'.'article_category' = 'arm_interest'.'category_id' group by 'arm_articles'.'article_id' having 'arm_articles'.'article_contributor_id' = 1322)

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.

  • 写回答

1条回答 默认 最新

  • dongshiran7000 2018-02-14 14:38
    关注
    • Add this configuration to your mysql database config in config/database.php:

         'mysql' => [
              ....
              'strict' => false,
              //'strict' => true,
          ],
      
    • Edit sql_mode in your mysql config file (my.ini or my.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
      
    • Restart your MySQL

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题