我有一個簡單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,每個用戶的地區教育意味着爲每個用戶選擇一種教育。 –