SELECT list is not in GROUP BY clause and contains nonaggregated column … incompatible with sql_mode=only_full_group_by

前端 未结 19 3353
灰色年华
灰色年华 2020-11-22 11:28

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          


        
19条回答
  •  再見小時候
    2020-11-22 11:44

    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 ids 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.

提交回复
热议问题