表名:table
数据:
id    uid    value
1     1     a
2     2     b
3     2     c
4     3     d
5     4     e
6     4     f去除uid相同的记录,保留相同记录中id最大的一条
查询结果:
id   uid   value
1    1      a
3    2      c
4    3      d
6    4      f
select * from table where 1 group by uid order by id desc这样查询出来的结果不是最新的记录,是保留最老的一条记录。

解决方案 »

  1.   

    select max(id),uid,value from table
    group by uid
      

  2.   

    DELETE A FROM `long` A INNER JOIN (SELECT UID,MAX(ID) AS MA FROM `long` GROUP BY UID) B ON A.UID=B.UID AND A.ID<>MA
      

  3.   

    max(id) uid     value
    1 1 a
    3 2 b
    5 3 dselect max(id),uid,value from `table`
    group by uid这是查询结果结果
      

  4.   

    select * from asda22 where id in 
    (select max(id) from asda22 group by uid)
      

  5.   

    select * from tt a where not exists(select 1 from tt where A.UID=B.UID AND A.ID<id)
      

  6.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  7.   

    select * from table a where not exists (select 1 from table where uid=a.uid and id>a.id)