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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33031596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:05:53  来源:igfitidea点击:

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

postgresqlsubquerysql-delete

提问by ArthurDatur

How do I delete only a few rows in postgreSQL? I want to fetch 10 rows to delete in a subquery.

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

My table

我的桌子

enter image description here

在此处输入图片说明

回答by Rahul Tripathi

You need to use a where condition as per your requirement like this:

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

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

or

或者

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

or you can try like this if you want to delete top 10 using ctid:

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

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

If you are looking to remove the duplicates from your table then try this:

如果您想从表中删除重复项,请尝试以下操作:

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

回答by Julo0sS

If you have some unique identifier (serial, let's call it "id") in your table, then just make something like :

如果您的表中有一些唯一标识符(串行,我们称其为“id”),那么只需创建如下内容:

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

Add or not something like "LIMIT 0,10"

添加或不添加类似“LIMIT 0,10”的内容