60

when i got upgraded my ubuntu from 15.10 to 16.04 i have this erro in my yii2 project

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3
of SELECT list is not in GROUP BY clause and contains nonaggregated column 
'iicityYii.opportunity_conditions.money' which is not functionally dependent 
on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The SQL being executed was:

SELECT SUM(oc.money),op.id,oc.money,
            op.mantaghe,
            op.`time`, op.`id`, `op`.`logo`,
           `pd`.`user_id`, `op`.`name`, 
           `pd`.`co_name`, `op`.`address`, 
           `op`.`project_type_id`, `op`.`state_id`
FROM `opportunity` op 
INNER JOIN `profile_details` pd  ON op.user_id=pd.user_id  
INNER JOIN `opportunity_conditions` oc ON   op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC

how to solve my problem ?

13 Answers 13

115

Run:

sudo mysql -u root -p
mysql> SELECT @@global.sql_mode;

(Then optionally copy the output to your notes somewhere in case you want to revert to those original settings later.)

And change the SQL Mode for your MySQL Server Instance:

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

(If you ever want to roll back, you could run something like mysql> SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; using the value you saved.)


A more permanent way (which will survive restarts of MySQL) would be using the MySQL configs. Go to /etc/mysql/my.cnf (or you may need to run sudo vim /etc/mysql/mysql.conf.d/mysql.cnf):

  • Add a section for [mysqld] and right below it add the statement sql_mode = "" or something like sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION".

  • Restart the MySQL service:

     sudo systemctl restart mysql
    

(or sudo service mysql restart)

See also https://dba.stackexchange.com/a/113153/18098

1
  • 2
    How to rollback ?
    – abenevaut
    Feb 5, 2018 at 17:05
104

In laravel with MySql go to file config/database.php and it change in array MySql mode strict to false.

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false, //from true
        'engine' => null,
    ],
],
4
  • 2
    Even the answer seems valid, I would recommend not to put images of code but the code itself. The OP can then just copy / paste without risk of misspelling during the copy.
    – рüффп
    May 17, 2018 at 16:34
  • I was super optimistic about this answer, but unfortunately it didn't help me, even if I also ran php artisan config:clear.
    – Ryan
    Jun 10, 2021 at 23:06
  • Actually, suddenly this did start working for me today, so thanks. I'm using WSL 1 with MySQL 8. But I'm not sure that I want to set "strict" to false because of stackoverflow.com/a/42104555/470749
    – Ryan
    Jun 14, 2021 at 0:25
  • The perfect solution even now for Laravel 9.x!
    – DreamBold
    Jul 21 at 8:25
17

The solution is to edit the MySQL config file because the config will revert after every restart...

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

and add

[mysqld]
sql-mode=""

then restart

sudo systemctl restart mysql

Works on ubuntu 18.04.

2
  • What are security implications of doing something like this? Oct 15, 2020 at 5:27
  • Works well on Ubuntu 18. Nov 25, 2021 at 6:18
15

Please just copy this line and run it. it worked for me.

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

Youtube video to fix this

4
  • 2
    Unfortunately the error returned again, but running the command again fixed it. Not sure why it came back.
    – Don King
    Apr 13, 2019 at 15:07
  • I put for you a youtube link in there please try to visit and watch how to fix
    – Humphrey
    May 10, 2019 at 13:55
  • 1
    The youtube video shows how to remove STRICT_ALL_TABLES in phpmyadmin. Removing this and saving did not solve the problem. No change at all. Running SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); as a SQL query in phpmyadmin solves the problem temporarily but it comes back automagically by itself after some time.
    – Don King
    Jun 3, 2019 at 20:27
  • 2
    I had to also add [mysqld] sql-mode="" to /etc/mysql/my.cnf Because without that when restarting mysql service issue came back again.
    – Mārcis P
    Mar 10, 2020 at 20:32
13

In select you have an aggregate function named sum and a set of column name, the error tell you that you have not specified the correct list of column name in group by clause. could be you should add more columns name in group by probably related to the profile_details, opportunity_conditions table

You have also ,(opportunity.id),(opportunity_conditions.money), (opportunity.mantaghe), why the () if you need sum you must add sum to all column

sum(opportunity.id), sum(opportunity_conditions.money),

sum(opportunity.mantaghe),

otherwise if this are normal columns you should use the normal syntax without ()

opportunity.id, opportunity_conditions.money,opportunity.mantaghe,

I have tried to rewrite a possible query

 SELECT SUM(opportunity_conditions.money),
        `opportunity`.`id`,
        `opportunity_conditions.money`,
        `opportunity.mantaghe`, 
        `opportunity`.`time`, 
        `opportunity`.`logo`, 
        `profile_details`.`user_id`,
        `opportunity`.`name`, 
        `profile_details`.`co_name`,
        `opportunity`.`address`, 
        `opportunity`.`project_type_id`,
        `opportunity`.`state_id` 
FROM `opportunity` 
INNER JOIN `profile_details` ON `opportunity`.`user_id`= `profile_details`.`user_id` 7
INNER JOIN `opportunity_conditions` ON `opportunity`.`id`=`opportunity_conditions`.`opportunity_id` 
GROUP BY`opportunity`.`id`,   `profile_details`.`user_id`,`opportunity_conditions.money`,  
ORDER BY `opportunity`.`id` DESC

with group by on the essential column name (i hope)

GROUP BY`opportunity`.`id`,   `profile_details`.`user_id`,`opportunity_conditions.money`,  
3

in laravel

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'turn.barnamehs.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by select * from tables where date(end_date) >= 2022-05-10 00:00:00 group by speciality_id

solution: change 'strict' => false, in config/database.php

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 11, 2022 at 5:36
2

It is a good idea to update your query to:

    SELECT SUM(any_value(oc.money)),op.id,any_value(oc.money) as money,
            op.mantaghe,
            op.`time`, op.`id`, `op`.`logo`,
           any_value(`pd`.`user_id`) as user_id, `op`.`name`, 
           any_value(`pd`.`co_name`) as co_name, `op`.`address`, 
           `op`.`project_type_id`, `op`.`state_id`
FROM `opportunity` op 
INNER JOIN `profile_details` pd  ON op.user_id=pd.user_id  
INNER JOIN `opportunity_conditions` oc ON   op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC

Understanding
When your query contains group by you should only select columns from the table you are grouping by, but if not, you ought to use the any_value() function to call the columns from other table.

I personally try not to update the sql_mode global variable.

You can read more about it on https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

1

Thx, this helped to me, but this 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 a MySQL restart:

Config File: /etc/mysql/my.cnf

[mysqld]
sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
1

Simply set strict false on following

Config > database.php  set 'strict' => false,
1

For CentOS8 I use webmin config file /etc/my.cnf.d/mysql-server.cnf

[mysqld]
sql-mode=""
0

You can use ANY_VALUE for the non-aggregated columns. So, you could use:

SELECT SUM(oc.money),
       op.id,
       ANY_VALUE(oc.money),
       ANY_VALUE(op.mantaghe),
       ANY_VALUE(op.`time`), op.`id`, ANY_VALUE(`op`.`logo`),
       ANY_VALUE(`pd`.`user_id`), ANY_VALUE(`op`.`name`), 
       ANY_VALUE(`pd`.`co_name`), ANY_VALUE(`op`.`address`), 
       ANY_VALUE(`op`.`project_type_id`), ANY_VALUE(`op`.`state_id`)
FROM `opportunity` op 
INNER JOIN `profile_details` pd  ON op.user_id=pd.user_id  
INNER JOIN `opportunity_conditions` oc ON   op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC

More details here.

0
sudo mysql -u root -p

mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
1
  • This answer essentially directs you to put duct tape over the check engine light.
    – reformed
    Jun 16 at 21:04
0

(Solved) Just include all selected columns in the Group By clause, Please have a look at bellow example

Invalid

SELECT o.custid, c.name, MAX(o.payment)
  FROM orders AS o, customers AS c
  WHERE o.custid = c.custid
  GROUP BY o.custid;

Valid (in this query we just include c.name also in group by)

SELECT o.custid, c.name, MAX(o.payment)
  FROM orders AS o, customers AS c
  WHERE o.custid = c.custid
  GROUP BY o.custid,**c.name**;

For more information please visit Main Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.