Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

SELECT list is not in GROUP BY clause and contains nonaggregated column … incompatible with sql_mode=only_full_group_by

I have this issue a lot because most of my solution moved to Mysql 8.0 from Mysql 5.6.

This issue is caused by the strict ANSI SQL rules that is applied when using GROUP BY.

You can set this settings off by using this command in your mysql terminal

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

With regard to your query, this means that if you  GROUP BY a column, then you can only select two things:

  • a column, or
  • aggregates of any other column


By "aggregates" of other columns, I mean using an aggregate function such as MIN()MAX(), or AVG() with another column. 

SELECT order_id,
       MAX(id) AS max_id,
       MAX(some_col),
       MIN(some_other_col)
FROM orders
WHERE id = '78' AND
      status = 'Active'
GROUP BY order_id

Note: The ANSI SQL extends what is allowed to be selected in GROUP BY by also including columns which are functionally dependent on the column(s) being selected. An example of functional dependency would be grouping by a primary key column in a table. Since the primary key is guaranteed to be unique for every record, therefore the value of any other column would also be determined. MySQL is one of the databases which allows for this (SQL Server and Oracle do not AFAIK).

 


comments powered by Disqus