如何删除除最大值之外的所有重复行?

时间:2013-12-24 13:07:24

标签: android sqlite

id name exp

test01 park 500

test01 park 700

test01 park 3000

test01 park 1500

test02 kim 700

test02 kim 500

test03 lee 500


我想删除除最大exp值之外的所有重复行。我想得到这样的结果:


id name exp

test01 park 3000

test02 kim 700

test03 lee 500


我尝试了一些sql语句但失败了。是否可以用一个sql语句实现我想要的东西?如果是这样,我该怎么做?

2 个答案:

答案 0 :(得分:2)

为什么要删除?只需使用如下查询显示适当的:

select id, name, max(exp) from mytable

更新: oops ..试试这个:

select id, name, max(exp) from mytable group by id, name

Android与否无关紧要。对于Android,您可以使用:execSQL(String sql)

答案 1 :(得分:0)

试试这个:

DELETE FROM mytable
  WHERE ROWID NOT IN ( SELECT t1.ROWID
                        FROM  mytable AS t1,
                              (SELECT id, name, MAX(exp) AS x FROM mytable GROUP BY id, name) AS t2
                          WHERE t1.id = t2.id AND t1.name = t2.name AND t1.exp = t2.x
                    );