【问题标题】:Laravel Sql syntax errorLaravel Sql 语法错误
【发布时间】:2017-04-15 17:43:04
【问题描述】:

这是我的 Laravel 查询:

$items =  DashboardItem::distinct()->where('dashboard_id',$dashboard->id)->groupBy('type')->get();

我在此查询中收到 SQLSTATE[42000]: Syntax error or access violation: 1055 错误。

错误:

SQLSTATE[42000]: Syntax error or access violation: 1055 'testapp.dashboard_items.id' isn't in GROUP BY (SQL: select distinct * from `dashboard_items` where `dashboard_id` = 3 group by `type`)

Sql 查询:

select distinct * from `dashboard_items` where `dashboard_id` = 3 group by `type`

但是在 PHPMyAdmin 中运行这个 sql 查询时,我得到了结果

【问题讨论】:

  • @Punit....同样的错误
  • 将您的查询生成器转换为 sql 字符串 echo DashboardItem::distinct()->where('dashboard_id',$dashboard->id)->groupBy('type')->toSql(); 进行调查
  • dashboard_id 是主键吗?
  • stackoverflow.com/questions/39138090/… 在这篇文章中找到了解决方案。
  • 我明白了......

标签: mysql laravel laravel-5.3


【解决方案1】:

试试这个

$items =  DB::table('dashboard_items')->where('dashboard_id',$dashboard->id)->groupBy('type')->distinct()->get();

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2015-03-22
    • 2015-04-04
    • 2016-12-13
    相关资源
    最近更新 更多