Home > Database > Mysql Tutorial > 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?

DDD
Release: 2024-12-22 16:40:13
Original
191 people have browsed it

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',''));
Copy after login
  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`
Copy after login
  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`
Copy after login

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.

The above is the detailed content of How to Resolve MySQL's 'SELECT list is not in GROUP BY clause' 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template