从表中删除第n行(postgresql)

时间:2017-03-30 12:58:51

标签: sql postgresql

我知道可以选择第n行。

例如

select from table limit 1 offset 3;

是否可以删除第n行?

1 个答案:

答案 0 :(得分:0)

如果您有id以及order by和/或partition bydelete可以row_number()使用drop table if exists t; create table t (id int, val int); insert into t values (1,9),(2,8),(3,7),(4,6),(5,5); delete from t where id in ( select id from ( select id, row_number() over (order by val asc) as rn from t ) s where s.rn = 3); select * from t;

+----+-----+
| id | val |
+----+-----+
|  1 |   9 |
|  2 |   8 |
|  4 |   6 |
|  5 |   5 |
+----+-----+

rextester演示:http://rextester.com/XJHB50704

返回:

at