Mysql DISTINCT not working if I add another column

后端 未结 2 805

mysql> select DISTINCT title, id from myadmins;

+------+------------+
| id   | title      |
+------+------------+
|    1 | admin      |
|    2 | stack      |
|           


        
2条回答
  •  一生所求
    2021-01-29 15:41

    DISTINCT applies to the entire row of data. Since the ID is different on each row, then you will end up with duplicate titles.

    If you need the ID, then you could use an aggregate to get the MAX(ID):

    select max(id) id,
      title
    from yourtable
    group by title
    order by id
    

    See SQL Fiddle with Demo

提交回复
热议问题