简体   繁体   中英

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 ?

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. Any idea how to fix that?

What is the best practice for this kind of operation?

Use 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. This might even make sense for non-integer types or with an increment <> 1.
For simple cases, the simple query is superior, though:

DELETE FROM test2 
WHERE  id BETWEEN 1 AND 1000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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