【问题标题】:Laravel 5.3: Select leftjion is not working with groupByLaravel 5.3:选择 leftjion 不适用于 groupBy
【发布时间】:2016-10-19 14:48:38
【问题描述】:

我想统计所有类别项目列表旁边的帖子数,如下所示:

  • 类别 1 (25)
  • 类别 2 (12)
  • 类别 3 (8)
  • 类别 ... (67)

在 Laravel 5.2 中,我使用以下代码来实现上述结果:

$cateCount = Category::leftjoin('posts', 'posts.icategoryid', '=', 'categories.id')
          ->groupBy('categories.id')
          ->get(['categories.id', 'categories.name', DB::raw('count(posts.icategoryid) as mycount')]);

但是,我升级到 Laravel 5.3,它显示以下错误:

SQLSTATE[42000]: 语法错误或访问冲突: 1055 'posts.categories.name' 不在 GROUP BY 中(SQL: 选择 categories.id, categories.name, count( posts.icategoryid) 作为我的计数来自categories left join posts on posts.icategoryid = categories.id group by categories.id)

你能告诉我如何解决这个问题或任何更好的方法来实现上述结果吗?

谢谢,万纳克

【问题讨论】:

    标签: laravel count group-by


    【解决方案1】:

    有一个类似的错误,在我的情况下,它与将在 5.3 中启用的 MySQL 严格模式有关:

    https://mattstauffer.co/blog/strict-mode-and-other-mysql-customizations-in-laravel-5-2

    'connections' => [
        'mysql' => [
            // Behave like MySQL 5.6
            'strict' => false,
    
            // Behave like MySQL 5.7
            'strict' => true,
        ]
    ]
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2017-06-22
      • 2012-07-15
      相关资源
      最近更新 更多