sql - 棘手的删除。我如何能?

标签 sql sql-delete

我有一个包含 2 列(均为 INT)的表,并且有 400 000 条记录(很多)。 第一列是按 ASC 顺序排列的随机数。第二列有一条规则(现在不重要) 表中有 1000 条记录,属于异常(exception)情况。因此,只有“-1”值单元格,而不是“规则”。

我怎样才能删除〜399 000条记录,所以我想在我的表中只留下那些带有-1的记录和它们的“邻居”(带有-1的记录之前和之后的记录)

更新 SQL Server 2k5 第一列值 - 是唯一的,但不是 ID-s(它不是​​++ :D)

示例:

之前:

 20022518   13
 20022882   364
 20022885   -1
 20022887   5
 20022905   18
 20023200   295
 20023412   212
 20023696   284
 20024112   416
 20025015   903
 20025400   385
 20025401   -1
 20025683   283
 20025981   298
 20025989   8
 20026752   763
 20027779   1027
 20028344   565
 20028350   6
 20028896   546
 20028921   25
 20028924   -1
 20028998   77
 20029031   33
 20029051   20
 20029492   441
 20029530   38
 20029890   360

之后:

 20022882   364
 20022885   -1
 20022887   5
 20025400   385
 20025401   -1
 20025683   283
 20028921   25
 20028924   -1
 20028998   77

最佳答案

如果我理解正确的话,您希望保留 col2 = -1 的所有记录以及与 -1 的记录最接近的 col1 的记录。假设 col1 中没有重复项我会做这样的事情

delete from table where not col1 in 
(
    (select col1 from table where col2 = -1)
union
    (select (select max(t2.col1) from table t2 where t2.col1 < t1.col1) from table t1 where t1.col2 = -1)
union
    (select (select min(t4.col1) from table t4 where t4.col1 > t3.col1) from table t3 where t3.col2 = -1)
)

编辑:
t4.col1 < t3.col1应该是t4.col1 > t3.col1
我用 col1 和 col2 创建了一个测试表,都是 int,col1 是 PK,但不是 autonumber

SELECT * from adjacent

给予

col1    col2
1   5
3   4
4   2
7   -1
11  8
12  2

使用上面的子选择:

SELECT * from adjacent
where
col1 in 
(
    (select col1 from adjacent where col2 = -1)
union
    (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
union
    (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
)

给出

col1    col2
4   2
7   -1
11  8

not还有

SELECT * from adjacent
where
col1 not in 
(
    (select col1 from adjacent where col2 = -1)
union
    (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
union
    (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
)

给出

col1    col2
1   5
3   4
12  2

最后删除并选择

delete from adjacent
where
col1 not in 
(
    (select col1 from adjacent where col2 = -1)
union
    (select (select max(t2.col1) from adjacent t2 where t2.col1 < t1.col1) from adjacent t1 where t1.col2 = -1)
union
    (select (select min(t4.col1) from adjacent t4 where t4.col1 > t3.col1) from adjacent t3 where t3.col2 = -1)
)

select * from adjacent

给出

col1    col2
4   2
7   -1
11  8

关于sql - 棘手的删除。我如何能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4196556/

相关文章:

java - JDBC 语句无法删除特定 MySql 表中的行

php - 如果 SQL IN 语句中的 Select 返回 NULL,则选择 ALL

sql - 使用createCriteria进行Grails子查询

mysql - 不同角色用户的最佳表结构

sql - 如何删除表中没有FK关系的所有行

mysql - 获取不同表mysql中的重复行

Oracle 12c - 删除表和所有关联的分区

mysql - 删除mysql并加入where

javascript - 如何使用 Codeigniter 从数据库调用图像?

java - sql删除没有错误但不要删除记录(除非我在Where子句中使用Firstname)