35

I updated mysql and I went from MySQL Version 5.6.17 to version 5.7.14

Since I have errors on my sql queries

Indeed, many of my queries look like this:

SELECT count (id) as nbr, lic from prep WHERE key = '18'

And I have this error:

1140 - In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'operator.preparation.orig_lic'; this is incompatible with sql_mode=only_full_group_by

After some research, I learn that Mysql 5.7.14 activates ONLY_FULL_GROUP_BY by default

Why is it enabled by default?

What is the best solution (for performance)? Disable ONLY_FULL_GROUP_BY or add a 'group by' on my query?

Thank you

Rocstar
  • 1,427
  • 3
  • 23
  • 41
  • 2
    only_full_group_by forces you to name all SELECT fields within the GROUP BY cause.. this way you can't write wrong GROUP BY querys that not name all fields... read this https://www.psce.com/en/blog/2012/05/15/mysql-mistakes-do-you-use-group-by-correctly/ – Raymond Nijland Aug 03 '17 at 12:23
  • 1
    @RaymondNijland Great post on GROUP BY mode 'ONLY_FULL_GROUP_BY'. I was searching for such a presentation. Thank you so much !!! – Swr7der Apr 10 '20 at 08:37

3 Answers3

41

only_full_group_by = on tells MySQL engine: Do not apply GROUP BY when you have doubt about what results to show and throw an error. Only apply it if Group By specifically tells you what to do. i.e. when the Group By is full and perfect!

only_full_group_by = off tells MySQL engine: always apply GROUP BY and if you have doubt about what results to choose, just pick one randomly!

You don't have to turn it off if you use GROUP BY properly!

Example:

Table: users

 id   |  name
----------------
  1      ali
  2      john
  3      ali

When you use GROUP BY on the name column:

SELECT * FROM users GROUP BY name;

There are two possible results:

  1      ali
  2      john     

OR

  2      john
  3      ali

MYSQL does not know what result to choose! Because there are different ids but both have name=ali.

Solution1:

only selecting the name field:

SELECT name FROM users GROUP BY name;

result:

  ali
  john     

This is a perfect solution. removing columns that makes GROUP BY confused. This means you know what you're doing. Usually, you do not need
those columns, but if you need them, go to Solution3!

Solution2:

Turning off only_full_group_by. MYSQL will pick one of the two possible results RANDOMLY!! (It's ok if you do not really care what id it will choose, but remember to turn it on immediately after your query to prevent unexpected behaviors in future groupBys)

Solution3

Use an Aggregate function like MIN(), MAX() to help MYSQL to decide what it must choose.

For example:

SELECT MAX(id), name FROM users GROUP BY name;

result:

  2      john     
  3      ali

It will choose the ali row which has the maximum id.

Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
  • 3
    **Thank you** for this brilliant explanation of what for years has seemed to me a bizarre, unhelpful, painful and arbitrary rule serving only to irritate developers. – Medlock Perlman Oct 03 '22 at 14:31
12

The "best" solution is to do the correct thing and fix your query by adding a group by, rather than override the error being thrown. If you override the error with ONLY_FULL_GROUP_BY the error you're experiencing will go away but you'll likely experience two new errors as a result of doing so:

  1. Unexpected results of including an aggregated value with non-aggregated values, the problem your error is trying to prevent.

  2. Inability to execute your query on other environments. If you ever need to switch settings or give your code to someone else not using this database, the query will throw the error again. If you get into a habit of overriding the error or other errors, your code could become unusable to others and severely cripple the usefulness of it.

In general, if you are receiving an error, fix it rather than just telling the compiler/optimizer to ignore it.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
yanman1234
  • 1,009
  • 9
  • 27
  • adding group by not always return what we want. For example, `SELECT sum(amount) as totalAmount, IF(action="+", amount, 0) as totalInGoing FROM `transactions` where account_id = 32099` this query, with ONLY_FULL_GROUP_BY mode, requires `GROUP BY action` to work. which makes totalInGoing useless because it will return severel rows. – Nuryagdy Mustapayev Dec 11 '20 at 07:56
  • Most people aren't trying to switch off something useful. I ended up here because I wanted to find out why what appeared to be a totally arbitrary and useless block (i.e. ONLY_FULL_GROUP_BY) was there in the first place. @ahmad-maboraki's [answer](https://stackoverflow.com/a/65680378/1490889) explains admirably what's going on, why ONLY_FULL_GROUP_BY is there, how to work with it rather than around it, and why that helps. – Medlock Perlman Oct 03 '22 at 14:24
7

The correct solution is to add the column you group by:

SELECT count (id) as nbr, lic 
from prep 
WHERE key = '18'
group by lic

for the performance this depends on the index you have.

The ONLY_FULL_GROUP_BY is the normal behavior for aggregation function in SQL and the adoption is 5.7 to avoid ambiguity on the casual result for non aggregated column result.

user3783243
  • 5,368
  • 5
  • 22
  • 41
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thank you but it changes something to do the group by on the id or on the lic ? – Rocstar Aug 03 '17 at 12:38
  • 1
    change completly .. the column you set in group by are usea as key for aggregation .. so eg: if you want th count of rows with the same lic youuse group by lic .. otherwise if you want the number of row for another column you don'u tse lic but the other column name .. if you case you are counting the number of not null id group by lic – ScaisEdge Aug 03 '17 at 12:41
  • Thank you but on query like this : **SELECT user_name, validated.user, via, master, nb_jours, min(date) as date_min from validated, users_instruction where is_finish = 0 and user = cust_id and users_instruction.br = validated.br group by master order by date_min ASC** I can't use Group by ? – Rocstar Aug 03 '17 at 13:07
  • Using group by you should have, in group by, all the columns not in aggregation function ( user_name , validated.user , via , master , nb_jours) not only master .. but this seems a new request .. try add a new question with a proper data sample and the expected result – ScaisEdge Aug 03 '17 at 13:34