"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Resolve MySQL's "SELECT list is not in GROUP BY clause" Error?

How to Resolve MySQL's "SELECT list is not in GROUP BY clause" Error?

Posted on 2025-03-24
Browse:349

How to Resolve MySQL's

Troubleshooting "SELECT list is not in GROUP BY clause" Error with MySQL

The error "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column" typically occurs in MySQL when the result of a query contains non-aggregated columns that are not included in the GROUP BY clause. This is due to the sql_mode=only_full_group_by setting, which enforces stricter grouping rules.

To address this issue, there are several solutions:

  1. Disable sql_mode=only_full_group_by: This can be done with the following command:
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
  1. Include all selected columns in the GROUP BY clause: This ensures that all columns in the SELECT list are included in the GROUP BY operation. For example:
SELECT *
FROM `tbl_customer_pod_uploads`
WHERE `load_id` = '78' AND
      `status` = 'Active'
GROUP BY `proof_type`, `id`
  1. Use aggregation functions: Instead of selecting individual columns, use aggregation functions such as SUM(), COUNT(), or AVG() to group the data. This eliminates the need for the GROUP BY clause. For example:
SELECT `proof_type`, COUNT(*) AS `document_count`
FROM `tbl_customer_pod_uploads`
WHERE `load_id` = '78' AND
      `status` = 'Active'
GROUP BY `proof_type`

It is important to note that changing the SQL mode is not the best practice. The preferred solution is to modify the query to comply with the stricter grouping rules.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3