查询表中相同数据显示id最大的数据

 

查询表中相同姓名id最大的数据

1. select a.* from `user` a,  (select name ,max(id) as max_id from `user` group by name) b where  a.id =b.max_id

 

2.select  * from `user` a where a.id in  (select max(id) from `user` group by name)

 

删除表中姓名相同的数据只保留最大id的数据

delete from  `user` where  id not  in (select a.id from (select max(id) id from `user` group by name) as a)

 

posted @ 2021-08-31 17:03  苑庆涛  阅读(803)  评论(0编辑  收藏  举报