删除所有行一定数量后

时间:2014-02-21 20:26:00

标签: sql sql-server

我有5000行数据。我想删除1000之后的所有行怎么做呢?

Delete from table
where *
limit 1000

这是正确的吗?

3 个答案:

答案 0 :(得分:2)

SQL Server

WITH cte
     AS (SELECT *,
                row_number() OVER (ORDER BY some_column) AS rn
         FROM   TABLE)
DELETE FROM cte
WHERE  rn > 1000 

答案 1 :(得分:1)

DELETE FROM TABLE
WHERE ID NOT IN
(
   SELECT ID FROM TABLE LIMIT 1000
)

此查询将:

1 - 从数据库中选择THE FIRST 1000条记录

2 - 删除前100条记录列表中不存在的所有记录

答案 2 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test ( A ) AS
SELECT LEVEL
FROM   DUAL
CONNECT BY LEVEL <= 5000;

查询1

SELECT COUNT(*) FROM test

<强> Results

| COUNT(*) |
|----------|
|     5000 |

查询2

DELETE FROM test
WHERE A IN ( SELECT A FROM (SELECT A, ROWNUM rn FROM test ) WHERE rn > 1000 )

<强> Results

查询3

SELECT COUNT(*) FROM test

<强> Results

| COUNT(*) |
|----------|
|     1000 |