MySQL Error: SELECT list is not in GROUP BY clause

前端 未结 4 2094
有刺的猬
有刺的猬 2020-11-29 14:18

I have a problem with my query and mysql throws the following error:

#1055 - Expression #66 of SELECT list is not in GROUP BY clause and 
contai         


        
4条回答
  •  萌比男神i
    2020-11-29 14:33

    If you want to use GROUP BY on specific column and get the id's of the results there is one nice trick how to do it:

    SELECT `distinct_column`, MAX(`id`), MAX(`another_int_column`) FROM `table_name` GROUP BY `distinct_column`
    

    Result:

    distinct_column  id       another_int_column
    Abertamy         13579    11
    Adamov           17765    14
    Adolfovice       2924     3
    Achotín          2241     2
    Babice           17772    14
    

    Now you have distinct values from distinct_column and with it the values of the corresponding row.

    source

    I think that the id and another_int_column have to be unique, but i'm not sure.

提交回复
热议问题