Mysql returning "Group By" error after upgrade to version 5.7

Asked

Viewed 16,210 times

3

I had a certain MYSQL query that was working correctly.

This is the consultation:

SELECT *, count(*) as count FROM `post_hashtags`
WHERE `created_at` BETWEEN ? AND ? 
    AND (SELECT count(*) AS `aggregate` 
FROM `post` 
WHERE `post`.`id` = post_hashtags.post_id 
    AND `status` = 0 
    AND `post_privacidade_id` = 1) >= ? 
GROUP BY `hashtag` 
ORDER BY `count` DESC, created_at DESC 
LIMIT 5

But after upgrading Mysql to version 5.7 on my machine, I started getting this error:

Syntax error or access Violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'post_hashtags.id' which is not functionally dependent on Columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Does anyone know what’s going on? I’ve never received this message before.

Have something considered wrong in the query, for the new version of Mysql?

  • Note: It’s good to get refactoring advice. I know my query is not great things there, but as it is an old system that is being maintained, I can not touch anything. I just really need to know how to make this mistake stop.

  • 1

    Maybe it’s dup of this => http://answall.com/q/164495/91

  • In what sense is this question duplicated? Where reports the error in the linked question?

2 answers

10

All fields that are in SELECT and are not using functions (such as COUNT, SUM, AVG, MAX, MIN, etc.) must be in the GROUP BY clause.

In your case, as you are using asterisk, you should have all fields of the table in the clause GROUP BY.

  • Wow, but isn’t that unfeasible? And why does this problem occur now and not before?

  • Version 5.7 is much more solid and robust, with stronger standards. I agree it’s more boring, but I like security.

10

I solved the problem by disabling the sql_mode.

I did so:

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

According to this reply from SOEN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.