AM using MySQL 5.7.13 on my windows PC with WAMP Server
Here my Problem is While executing this query
SELECT *
FROM `tbl_customer_pod_uploads`
WHERE
Search for "SQL mode" if you are using PhpMyAdmin and take off the value: ONLY_FULL_GROUP_BY
, just did and it okay.
Turn off only_full_group_by
only if you know what you're doing!
A simple example to demonstrate the issue:
Table: users
id | name
----------------
1 ali
2 john
3 ali
When you use GROUP BY
on 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 id
s but both have name=ali
.
Solution1:
only selecting the name
field:
SELECT name FROM users GROUP BY name;
result:
ali
john
Solution2:
Turning off only_full_group_by
. MYSQL will show you one of the two possible results I showed earlier RANDOMLY!! (It's ok if you do not really care what id
it will choose)
Solution3
Use an Aggregate
function like MIN()
, MAX()
to help MYSQL to decide what it must choose.
for example I want the minimum id
:
SELECT MIN(id), name FROM users GROUP BY name;
result:
1 ali
2 john
It will choose the ali
row which has the minimum id
.
Update for MySQL 8.0
Your sql-mode
will not have NO_AUTO_CREATE_USER
as it has been removed as mentioned here - how-to-set-sql-mode-in-my-cnf-in-mysql-8
[mysqld]
sql-mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
Also if someone doesn't have a my.cnf
file then they create a new one in /etc/my.cnf
and then add the above lines.
Below method solved my problem:
In ubuntu
Type: sudo vi /etc/mysql/my.cnf
type A to enter insert mode
In the last line paste below two line code:
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Type esc to exit input mode
Type :wq to save and close vim.
Type sudo service mysql restart
to restart MySQL.
In your my.ini
, write this:
[mysqld]
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
depend on your version. Or:
[mysqld]
sql_mode = ""
or simply remove this: ONLY_FULL_GROUP_BY
(1) PHPMyAdmin
if you are using phpMyAdmin then change the "sql_mode" setting as mentioned in the below screenshot.
Edit "sql mode" variable and remove the "ONLY_FULL_GROUP_BY" text from the value
(2) SQL/Command prompt Run the below command.
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
(3) Don't use SELECT *
Use relevant column in SELECT query. relevant means columns, which are either coming in "group by" clause or column with the aggregate function (MAX, MIN, SUM, COUNT etc)
Changes made by using point(1) OR point(2) will not set it PERMANENTLY, and it will revert after every restart.
So you should set this in your config file (e.g. /etc/mysql/my.cnf in the [mysqld] section), so that the changes remain in effect after MySQL restart:
Config File: /etc/mysql/my.cnf
Variable name : sql_mode OR sql-mode