PostgreSQL DELETE FROM(SELECT * FROM table FETCH FIRST 10 ROWS ONLY)

时间:2015-10-09 06:45:01

标签: postgresql subquery sql-delete

如何在postgreSQL中只删除几行? 我想在子查询中获取要删除的10行。

我的表

enter image description here

2 个答案:

答案 0 :(得分:22)

您需要根据您的要求使用where条件,如下所示:

delete from mytable where id in(1,2,3,4,5,6,7,8,9,10)

delete from mytable where id in(select id from mytable where someconditon)

如果您想使用ctid删除前10名,可以尝试这样做:

DELETE FROM mytable 
WHERE ctid IN (
    SELECT ctid
    FROM mytable 
    GROUP BY s.serialId, s.valuetimestamp
    ORDER BY s.serialId
    LIMIT 10
)

如果您要删除表格中的重复项,请尝试以下操作:

DELETE FROM mytable
 WHERE ctid NOT IN
  (SELECT MAX(s.ctid)
    FROM table s
    GROUP BY s.serialId, s.valuetimestamp);

答案 1 :(得分:0)

如果你有一些独特的标识符(序列号,让我们在你的表中调用它" id"),那么只需输入:

DELETE FROM table WHERE table.id IN (SELECT table.id FROM table WHERE *whatever*)

添加或不包含" LIMIT 0,10"