I was trying to get the name of Users in my system and I use the following code on PHP Laravel:
$course_assessments = CourseAssessment::where('canvas_assignment_id', '=', $canvas_assignment_id)
->join('assessment_submissions', 'assessment_submissions.courses_assessments_id', '=', 'course_assessments.id')
->join('users', 'users.id', '=', 'assessment_submissions.evaluated_user')
->groupby('users.name')
->get();
When I run the code I got the following error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel.course_assessments.id' isn't in GROUP BY (SQL: select * from `course_assessments` inner join `assessment_submissions` on `assessment_submissions`.`courses_assessments_id` = `course_assessments`.`id` inner join `users` on `users`.`id` = `assessment_submissions`.`evaluated_user` where `canvas_assignment_id` = 248302 group by `assessment_submissions`.`evaluated_user`)
But if I get the SQL code from the error above:
select * from `course_assessments` inner join `assessment_submissions` on `assessment_submissions`.`courses_assessments_id` = `course_assessments`.`id` inner join `users` on `users`.`id` = `assessment_submissions`.`evaluated_user` where `canvas_assignment_id` = 248302 group by `assessment_submissions`.`evaluated_user`
and I run the code on PhpMyAdmin, the code it works, but show me this warning message:
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
How can I fix that on Laravel?
2
Answers
When you group rows with GROUP BY, the database doesn’t know which row should be selected. You have to specifically tell the database which row to be shown, for example,
FIRST(name)
will select the first row of column name.Except the column in GROUP BY, which will be unique.
So in your query,
select *
won’t work, you should addselect()
and specify the field and aggregate function.users.name
is in GROUP BY so it doesn’t need aggregate function, other fields need aggregate function.Try this:
From:
To:
I am assuming assessment_submissions table contains canvas_assignment_id column.