Error
Error Code:
1140
MariaDB Error 1140: Illegal Column Mix in Query
Description
This error occurs in MariaDB when a SELECT statement attempts to combine aggregate functions (like MIN(), MAX(), COUNT()) with non-aggregated columns without specifying a GROUP BY clause. MariaDB cannot determine how to group the non-aggregated data for the aggregate results, leading to an ambiguous query structure.
Error Message
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
Known Causes
3 known causesMissing GROUP BY Clause
A SELECT statement includes aggregate functions and non-aggregated columns without specifying how to group the non-aggregated data.
Incorrect Query Logic
The query attempts to select individual column values alongside aggregated results without a logical grouping context, leading to ambiguity.
Incomplete GROUP BY
Some non-aggregated columns are included in the SELECT list but are not present in the GROUP BY clause, making the query ambiguous.
Solutions
4 solutions available1. Add a GROUP BY Clause easy
Explicitly tell MariaDB how to group rows when using aggregate functions.
1
Identify the non-aggregated columns in your SELECT list. These are the columns you want to group by.
SELECT column1, column2, COUNT(*) FROM your_table;
2
Add a GROUP BY clause to your query, listing all the non-aggregated columns.
SELECT column1, column2, COUNT(*) FROM your_table GROUP BY column1, column2;
2. Remove Non-Aggregated Columns easy
Simplify the query by removing any columns that are not part of an aggregate function and not in a GROUP BY clause.
1
Review your SELECT statement. Identify columns that are not used in aggregate functions (like MIN(), MAX(), COUNT(), SUM(), AVG()).
SELECT column1, COUNT(*) FROM your_table;
2
Remove these non-aggregated columns from the SELECT list if they are not intended for grouping.
SELECT COUNT(*) FROM your_table;
3. Use an Aggregate Function for All Columns medium
Apply an aggregate function to every column in the SELECT list if you truly intend to operate on the entire dataset without grouping.
1
Examine your SELECT statement. If you want to get a single result representing the whole table, apply an aggregate function to each column that needs to be summarized.
SELECT column1, MIN(column2) FROM your_table;
2
For columns where a specific aggregate doesn't make sense (e.g., a primary key), you might consider using MIN() or MAX() to get a representative value, or exclude it if it's not relevant to your aggregation goal.
SELECT MIN(column1), MAX(column2) FROM your_table;
4. Ensure Correct Usage with Subqueries or Derived Tables advanced
When using subqueries, ensure they either have a GROUP BY clause or do not mix aggregated and non-aggregated columns.
1
If your query involves a subquery or derived table, check the SELECT statement within that subquery. This is a common place for the error to originate.
SELECT t1.colA, sub.count_col FROM table1 t1 JOIN (SELECT colB, COUNT(*) as count_col FROM your_table GROUP BY colB) AS sub ON t1.colA = sub.colB;
2
Ensure the subquery adheres to the same rules: either it has a GROUP BY clause if it mixes aggregated and non-aggregated columns, or all columns are aggregated.
SELECT t1.colA, sub.count_col FROM table1 t1 JOIN (SELECT COUNT(*) as count_col FROM your_table) AS sub ON t1.colA = sub.colB; -- Example of aggregating all rows in subquery