我有一个简单Education
模型工作包含以下字段:GROUPBY不正确的口才和查询设计器laravel
e_id // Primary key
user_id //foreign key to link to User model
field
grade
university
country_education
city_education
date_of_graduation
这是我的模型结构:
class Education extends Model
{
public $primaryKey = 'e_id';
public $timestamps = false;
protected $table = 'educations';
protected $fillable = ['user_id', 'field', 'grade', 'university', 'country_education', 'city_education', 'date_of_graduation'];
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'user_id');
}
}
现在我想根据user_id
字段选择不同的行。到我写这篇:
$educations = Education::select(['e_id', 'user_id', 'field', 'grade'])->groupBy(['user_id']);
但低于错误发生:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lms_forms.educations.e_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `e_id`, `user_id`, `field`, `grade` from `educations` group by `user_id` limit 10 offset 0)
然后我加入e_id
主键groupBy(['user_id'])
:
$educations = Education::select(['e_id', 'user_id', 'field', 'grade'])->groupBy(['user_id','e_id']);
查询运行,但返回所有记录,而不管user_id
的区别。
什么是问题,我该怎么办?
MYSQL5.7已经在这方面作出一些改变检查迁移注HTTPS://dev.mysql。 com/doc/refman/5.7/en/faqs-migration.html您真的应该阅读MYSQ提供的所有错误消息,线索在错误消息中 – RiggsFolly
您想要选择什么?不同的用户?我猜用户可能有多个字段/等级? –
@Olaf Dietsche,每个用户的地区教育意味着为每个用户选择一种教育。 –