Disable "sql-mode = only_full_group_by"

2

I installed mysql in my note and I'm using Linux Mint 18.2. When I run the query:

SELECT *,SUM(ValorTotalProdutos) AS ValorTotal, SUM(QtdProdutos) AS QtdFinal FROM tabela WHERE SESSIONID = '77c2c1de02e94581a459ed2d4fa76370' AND StatusCompras = 'A'

The error appears:

#1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'banco.tabela.IDCarrinho'; this is incompatible with sql_mode=only_full_group_by

I opened the file my.cnf

sudo nano /etc/mysql/my.cnf

And includes at the end of the file:

sql-mode=""

I restarted mysql:

sudo service mysql restart

And it made a mistake. So I deleted the above command and added another line:

sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

I restarted and the same error appeared. The error that appeared in both is:

sudo service mysql restart
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

How do I disable sql-mode?

    
asked by anonymous 22.08.2017 / 00:28

2 answers

4

The options for changing SQL MODE are as follows:

Using this from the command line when starting the server:

--sql-mode="modes"

Or in the configuration file:

sql-mode="modes"

At _runtime:

SET GLOBAL sql_mode = 'modes';
SET SESSION sql_mode = 'modes';

It's important to make sure of all the options, because you can have some of the highest priority overriding the one you changed manually (or changed one file, and the option being set in another).

For example, even if you specify the option in my.cnf or equivalent, if the script that initializes the server makes the command line option, it will prevail.

Note that this default option was changed in version 5.7.5 of MySQL.

Also, remember to only change the mode that is affecting your use, keeping other options as they are to avoid side effects.

Now, if possible, review the query logic, eventually it may be a more definitive and portable solution to the problem.

More details in the manual:

  

link

  

link

    
22.08.2017 / 00:47
0

You can perform bank settings at runtime in PHP. It's more practical, because if you need to switch to another server you do not have to redo the settings again.

If you are using PDO execute this query immediately after instantiating the PDO object:

$ Pdo-> exec ("SET sql_mode = (SELECT REPLACE (@@ sql_mode, 'ONLY_FULL_GROUP_BY', ''));");

See the example:

<?php
namespace lib;
class Dao
{
    private static $Dao;
    private static $Pdo;

    final private function __construct()
    {
        try {
            self::$Pdo = new \PDO("mysql:host=" . CONEXAO['host'] . ";dbname=" . CONEXAO['database'], CONEXAO['user'], CONEXAO['password']);
            self::$Pdo->exec("SET TIME_ZONE = '-03:00'");
            self::$Pdo->exec("set names " . APP_CHARSET);
            self::$Pdo->exec("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
            self::$Pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            self::$Pdo->query("SET group_concat_max_len = " . GROUP_CONCAT_MAX_LEN);
        } catch (\PDOException $ex) {
            throw new SystemExceptions($ex->getMessage());
        }
    }

    //...

}
    
28.06.2018 / 22:52