I'm currently using groupBy
to group concatenated (book name and book_author). I'm intentionally using groupBy
because I have other columns to get their average, and sum.
I initially used this code below. But it returning me an error.
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
Reference::select('*')
->selectRaw("CONCAT(book_name, ' ', book_author) AS book")
->groupBy('type');
If I add an id
column inside groupBy, it does not returning an error. But it does not order, I still getting duplicate book
.
Reference::select('*')
->selectRaw("CONCAT(book_name, ' ', book_author) AS book")
->groupBy(['id', 'type']);
Someone, how to achieve this properly?