简体   繁体   English

使用 generate_series() 删除行范围?

[英]Delete range of rows using generate_series()?

To update every row in a table, I use this query (and it's working):要更新表中的每一行,我使用此查询(并且它正在工作):

在此处输入图像描述

I wonder if I can use a similar notation for DELETE ?我想知道我是否可以对DELETE使用类似的符号?

Something like:就像是:

DELETE from test2
from generate_series(1, 1000) as idx
WHERE id = idx;

Won't work as the second FROM is invalid, of course.当然,因为第二个FROM无效,所以不起作用。 Any idea how to fix that?知道如何解决吗?

What is the best practice for this kind of operation?这种操作的最佳实践是什么?

Use USING :使用USING

DELETE from test2 
       using generate_series(1, 1000) idx
       WHERE id = idx

What is the best practice for this kind of operation?这种操作的最佳实践是什么?

You can use generate_series() like Gordon demonstrates.可以像 Gordon 演示的那样使用generate_series() This might even make sense for non-integer types or with an increment <> 1.这甚至可能对非整数类型或增量 <> 1 有意义。
For simple cases, the simple query is superior, though:但是,对于简单的情况,简单查询更胜一筹:

DELETE FROM test2 
WHERE  id BETWEEN 1 AND 1000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM