MySQL #1140 - Mixing of GROUP columns

前端 未结 7 1198
难免孤独
难免孤独 2020-11-29 11:27

Hi wondering if perhaps someone could shed some light on the below error. The sql works fine locally but i get the the below error remotely.

SQL query:

<         


        
相关标签:
7条回答
  • 2020-11-29 11:40

    In Laravel Framework, setting 'strict' => false, in database file in the config folder will fix this.

    0 讨论(0)
  • 2020-11-29 11:43

    Your server probably has ONLY_FULL_GROUP_BY turned on. You can either turn it off or add each of your selected fields to the group by.

    0 讨论(0)
  • 2020-11-29 11:43

    I have the same problem in old version of MySQL 5.0. In newer 5.5 version it works!

    SELECT COUNT(*), id
    FROM `cities` AS `c`
    
    0 讨论(0)
  • 2020-11-29 11:46

    when you use aggregate functions in sql like sum(),min(),max() you must be use group by

    But in latest sql u have to use all the columns in select as a group By too.

    for this in laravel project you use in config->database

    'strict' => false,

    and clear config cache by running php artisan config:cache.

    i think it will be more helpfull for you.

    0 讨论(0)
  • 2020-11-29 11:48

    stripped it back to basics:

       SELECT COUNT( node.nid ) 
         FROM node node
    LEFT JOIN content_type_update node_data_field_update_date ON node.vid = node_data_field_update_date.vid
        WHERE node.type IN ('update')
     ORDER BY node_data_field_update_date.field_update_date_value DESC
    
    0 讨论(0)
  • 2020-11-29 11:58

    The reason a single column using an aggregate function works while the version with columns not using aggregate functions doesn't is because you need to specify a GROUP BY clause. Here's what your query should look resemble:

       SELECT COUNT(n.nid), 
              n.nid, 
              ctu.field_update_date_value
         FROM NODE n
    LEFT JOIN CONTENT_TYPE_UPDATE ctu ON ctu.vid = n.vid
        WHERE n.type IN ('update')
     GROUP BY n.nid, ctu.field_update_date_value
     ORDER BY field_update_date_value DESC
    

    I changed out your table aliases for shorter ones - easier to read. Here's the meat of your issue:

       SELECT n.nid,
              COUNT(n.fake_example_column),                
              ctu.field_update_date_value
          ...
     GROUP BY n.nid, ctu.field_update_date_value
    

    I altered the example to use a fake column in order to highlight how the GROUP BY needs to be defined. Any column you reference without wrapping in an aggregate function should to be mentioned in the GROUP BY. I say should because MySQL is the only db I'm aware of that supports a GROUP BY where you can selectively omit columns - there are numerous SO questions about why queries work on MySQL but can't be ported without change to other dbs. Apparently in your case, you still need to define at least one.

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