Postgresql,将 CSV 字符串转换为数组

bre*_*one 2 postgresql function

Postgresql 版本 12。在一个函数中,想要删除具有特定 ID 的用户(列“id”bigint)。ID 作为 CSV 字符串 (VARCHAR) 传入,如下所示:

'1,2,3'
Run Code Online (Sandbox Code Playgroud)

函数是这样的:

remove_users(in ids varchar)
Run Code Online (Sandbox Code Playgroud)

并在函数中想做:

delete from users where users.id in ids
Run Code Online (Sandbox Code Playgroud)

或者

delete from users where users.id = any(array _ids)
Run Code Online (Sandbox Code Playgroud)

如何从 csv 字符串转换为 int 数组?

Boh*_*ian 5

使用内置数组函数:

delete from user
where id any(string_to_array('1,2,3', ','))
Run Code Online (Sandbox Code Playgroud)