'Syntax error or access violation in Laravel and MySql
I have the following code in Laravel
$total_for_indicators_per_date = Ed_data_count::where('ed_indicator_id', $indicator_id)
->orderby('date', 'asc')
->selectRaw("sum(count_c) as sum_x,DATE_FORMAT(date,'%b-%y') date_x")
->groupBy('date')->get();
When trying this code on the server, I get the following error.
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
I know that we can overcome this issue by making strict option disabled in the configuration files, but this solution is very dangerous from a security viewpoint.
I have tried to set the strict option for only the request, using the following code, but it seems nothing has solved
Config::set('database.connections.mysql.strict', false);
$total_for_indicators_per_date = Ed_data_count::where('ed_indicator_id', $indicator_id)
->orderby('date', 'asc')
->selectRaw("sum(count_c) as sum_x,DATE_FORMAT(date,'%b-%y') date_x")
->groupBy('date')->get();
It seems that the query running using enabled strict, although I set it as false.
The question is where the issue in the last technique, which I have used.
PS : I don't prefer to make "order by" after "group by".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
