Home > Database > Mysql Tutorial > How to Fix the \'Expression of SELECT List Not in GROUP BY Clause\' MySQL Error?

How to Fix the \'Expression of SELECT List Not in GROUP BY Clause\' MySQL Error?

Mary-Kate Olsen
Release: 2024-11-25 06:14:15
Original
265 people have browsed it

How to Fix the

Troubleshooting "Expression of SELECT List Not in GROUP BY Clause" Error

The error, "Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column 'libelle'," is typically encountered when executing a query with a non-aggregated column in the SELECT list and GROUP BY clause. To resolve this error, it is necessary to ensure that all non-aggregated columns are included in the GROUP BY clause or are aggregated using functions such as SUM(), AVG(), or COUNT().

In this specific case, the query attempts to select columns including 'libelle', 'credit_initial', 'disponible_v', and 'montant'. However, only 'libelle' is included in the GROUP BY clause, while 'montant' is non-aggregated. To fix this issue, modify the query to either include 'montant' in the GROUP BY clause or aggregate it using SUM() or another appropriate function.

SOLUTION

Here is an updated version of the query that includes 'montant' in the GROUP BY clause:

SELECT libelle, credit_initial, disponible_v, SUM(montant) AS total_montant 
FROM fiche, annee, type 
WHERE type.id_type = annee.id_type 
AND annee.id_annee = fiche.id_annee 
AND annee = YEAR(current_timestamp) 
GROUP BY libelle, credit_initial, disponible_v 
ORDER BY libelle ASC
Copy after login

Alternately, the query can be adjusted to aggregate 'montant' using SUM():

SELECT libelle, credit_initial, disponible_v, SUM(montant) 
FROM fiche, annee, type 
WHERE type.id_type = annee.id_type 
AND annee.id_annee = fiche.id_annee 
AND annee = YEAR(current_timestamp) 
GROUP BY libelle 
ORDER BY libelle ASC
Copy after login

By making these changes, the query will conform to MySQL 5.7's strict GROUP BY mode and avoid the error.

The above is the detailed content of How to Fix the \'Expression of SELECT List Not in GROUP BY Clause\' MySQL Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template