Error related to only_full_group_by when executing a query in MySql

前端 未结 18 2549
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:39

I have upgraded my system and have installed MySql 5.7.9 with php for a web application I am working on. I have a query that is dynamically created, and when run in older ve

相关标签:
18条回答
  • 2020-11-21 04:54

    You can add a unique index to group_id; if you are sure that group_id is unique.

    It can solve your case without modifying the query.

    A late answer, but it has not been mentioned yet in the answers. Maybe it should complete the already comprehensive answers available. At least it did solve my case when I had to split a table with too many fields.

    0 讨论(0)
  • 2020-11-21 04:56

    The consensus answer above is good but if you've got problems running queries within stored procedures after fixing your my.cnf file, then try loading your SPs again.

    I suspect MySQL must have compiled the SPs with the default only_full_group_by set originally. Therefore, even when I changed my.cnf and restarted mysqld it had no effect on the SPs, and they kept failing with "SELECT list is not in GROUP BY clause and contains nonaggregated column ... which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by".

    Reloading the SPs must have caused them to be recompiled now with only_full_group_by disabled. After that, they seem to work as expected.

    0 讨论(0)
  • 2020-11-21 04:58

    Apologies for not using your exact SQL

    I used this query to overcome the Mysql warning.

    SELECT count(*) AS cnt, `regions_id`
    FROM regionables 
    WHERE `regionable_id` = '115' OR `regionable_id` = '714'
    GROUP BY `regions_id`
    HAVING cnt > 1
    

    note the key for me being

    count(*) AS cnt
    
    0 讨论(0)
  • 2020-11-21 04:59

    If you don't want to make any changes in your current query then follow the below steps -

    1. vagrant ssh into your box
    2. Type: sudo vim /etc/mysql/my.cnf
    3. Scroll to the bottom of file and type A to enter insert mode
    4. Copy and paste

      [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
      
    5. Type esc to exit input mode

    6. Type :wq to save and close vim.
    7. Type sudo service mysql restart to restart MySQL.
    0 讨论(0)
  • 2020-11-21 05:00

    Addition of lines (mention below) in file : /etc/mysql/my.cnf

    [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
    

    Work fine for me. Server version: 5.7.18-0ubuntu0.16.04.1 - (Ubuntu)

    0 讨论(0)
  • 2020-11-21 05:02

    If you have this error with Symfony using doctrine query builder, and if this error is caused by an orderBy :

    Pay attention to select the column you want to groupBy, and use addGroupBy instead of groupBy :

    $query = $this->createQueryBuilder('smth')->addGroupBy('smth.mycolumn');
    

    Works on Symfony3 -

    0 讨论(0)
提交回复
热议问题