一个删除问题
本帖最后由 tmj3465968 于 2013-12-24 18:19:51 编辑 表 t1(name,number)
数据如下
name number
10086 0
10086 96
10000 0
我希望就是删除 number=0的数据 但是如果 name没有相同的就不删除, 也就是删除 第一条数据。
该怎么写呢。。
[解决办法]
delete from t1 a
where a.number = 0
and a.name in
(select name from t1 group by name having count(1) > 1);
DELETE FROM T1 WHERE T1.NUM=0 AND EXISTS(SELECT 1 FROM T1 T2 WHERE T1.NAME=T2.NAME AND T2.NUM<>0)
--删除而留一条name相同的记录
delete from t1
where rowid in (select rid
from (select name,
row_number() over(partition by name order by number desc) as rn,
rowid as rid
from t1)
where rn > 1)
and number = 0;
DELETE FROM t1 WHERE NUMBER=0 AND NAME IN
(SELECT NAME FROM t1 GROUP BY NAME HAVING COUNT(1)>1)