Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code: 1055 incompatible with sql_mode=only_full_group_by

I have been having issues switching to an offline version of the Lahman SQL baseball database. I was using a terminal embed into an EDX course. This command runs fine on the web terminal:

SELECT concat(m.nameFirst,concat(" ",m.nameLast)) as Player,
    p.IPOuts/3 as IP,
    p.W,p.L,p.H,p.BB,p.ER,p.SV,p.SO as K,
    p.IPOuts+p.W*5+p.SV+p.SO-p.BB-p.L-p.H as PTS,
    p.yearID as Year
FROM Pitching p
Inner Join Master m
    ON p.playerID=m.playerID
WHERE p.yearID=2014 AND p.IPOuts>=50
GROUP BY m.playerID
ORDER BY PTS DESC;

Which is running SQL 5.5.46, but when I use my offline version running 5.7.10 I get the following error code:

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'stats.m.nameFirst' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

I've been reading a lot of solutions to people's problems, but they haven't helped in this case. That's never happened before, so I think this is either super obvious or maybe I'm getting ok at coding. Anyway, anyone know how to fix this?

like image 895
Frydaddy07 Avatar asked Mar 24 '16 18:03

Frydaddy07


People also ask

How do I fix error 1055?

If you've recently started getting 1055 errors, what are your choices? fix the offending SQL queries, or get their authors to do that. roll back to a version of MySQL compatible out-of-the-box with the application software you use. change your server's sql_mode to get rid of the newly set ONLY_FULL_GROUP_BY mode.

What is sql_mode Only_full_group_by?

If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.

How do I view SQL mode in MySQL?

To determine the current value of the session or global SQL mode, use these statements: SELECT @@SESSION. sql_mode; SELECT @@GLOBAL.


3 Answers

In 5.7 the sqlmode is set by default to:

 ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION 

To remove the clause ONLY_FULL_GROUP_BY you can do this:

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

This supposed you need to make that GROUP BY with non aggregated columns.

Regards

like image 155
White Feather Avatar answered Sep 22 '22 22:09

White Feather


The accepted solution above didn't work for me on version 5.7.9, for osx10.9 (x86_64).

Then the following worked -

set global sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; 
like image 32
Anis Avatar answered Sep 22 '22 22:09

Anis


For other use cases: You don't necessarily have to disable ONLY_FULL_GROUP_BY Given a case like this, According to mysql docs, "This query is invalid if name is not a primary key of t or a unique NOT NULL column. In this case, no functional dependency can be inferred and an error occurs:"

SELECT name, address, MAX(age) FROM t GROUP BY name;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP
BY clause and contains nonaggregated column 'mydb.t.address' which
is not functionally dependent on columns in GROUP BY clause; this
is incompatible with sql_mode=only_full_group_by

Instead you can use this ANY_VALUE('my_column_name') my_column_name Quoting the mysql docs, "In this case, MySQL ignores the nondeterminism of address values within each name group and accepts the query." Use ANY_VALUE() to refer to address:

SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;
like image 42
phil Avatar answered Sep 22 '22 22:09

phil