表里面有这样的记录:
id  ord   name   weight
2    1    大白菜   5
3    2    大白菜   5
4    3     土豆    6
id是主键。
我想在JSP里面写段代码,删除相同的,只保留一个记录。比如这里,我只保留大白菜一条记录,而删除掉另外一条。

解决方案 »

  1.   

    select a.* from table a join  
    (select min(id),name from table group by name) b 
    on a.id = b.id
      

  2.   

    --删除相同记录
    delete from table where id not in(select min(id) as num from table group by name)
      

  3.   

    自查询,按name分组查询出ID最小的name 相同的一条记录
    select min(id) as num from table group by name然后删除表table中id不在上面子查询结果中的记录,也就是把名字相同的记录删掉
    delete from table where id not in(select min(id) as num from table group by name)
      

  4.   

    谢谢拉。搞定了,看来SQL语法我要加强一下了。还是很重要的
      

  5.   

    delete from table where id not in(select min(id) as num from table group by name)