如何根据字符串id删除PostgreSQL数据库中的多行?

Q L*_*Liu 3 sql postgresql types

所以我有下表:

qliu=# select id from api_member;
 id  
-----
 242
 236
 246
 251
 253
 9
 21
 185
 49
 188
Run Code Online (Sandbox Code Playgroud)

我希望能够使用以下命令删除具有一系列ID的行:

delete from api_member where id between '0' and '10';
Run Code Online (Sandbox Code Playgroud)

但它什么都没删除.如果你想知道的话

delete from api_member where id between 0 and 10;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ERROR:  operator does not exist: character varying >= integer
LINE 1: delete from api_member where id between 0 and 10;
                                        ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

das*_*ght 9

这是因为您的列id是类型varchar,而不是整数.您可以通过将其转换为此来解决此问题integer,如下所示:

delete from api_member
where CAST(id as integer) between 0 and 10;
Run Code Online (Sandbox Code Playgroud)

如果行数很大,则此操作可能太慢.请考虑将id列的类型更改为数字类型.