【发布时间】:2019-02-05 14:45:17
【问题描述】:
我正在建立一个论坛。我有两个模型:线程和报告。
一个话题可以被不同的用户多次报告。
我正在制作一个页面来显示已向版主报告的主题。
现在的问题是,我将按线程对报告进行分组,以便显示单个线程被报告了多少次,并且如果多次报告同一线程就不会出现多次。
报告表:
$table->increments('id');
$table->unsignedInteger('thread_id');
线程表:
$table->increments('id');
$table->string('title', 500);
关系报告 -> 话题
public function thread() {
return $this->hasOne(Thread::class, 'id');
}
所以我这样做:
Report::with('thread')
->groupBy('thread_id')
->get();
但是,我收到此错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reports.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from `reports` group by `thread_id`)
知道如何解决此问题或以其他方式解决此问题吗?谢谢!
【问题讨论】:
-
Group by clause in mySQL and postgreSQL, why the error in postgreSQL?
sql_mode=only_full_group_by需要用 agg 函数包装每一列。 -
如果不了解您的意图,就很难说。在不选择聚合(总和、计数等)的情况下按某些内容进行分组是没有任何意义的。你能详细解释一下你正在尝试做什么/实现什么吗?
标签: mysql laravel eloquent query-builder laravel-query-builder