如何将数值转换为sql中的文本以进行选择查询

Spi*_*ike 3 postgresql postgresql-9.1

我有这样的字符串值

'[123, 124]'
Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令修剪值

select trim('[123, 124]', '[]'); --returning '123, 124' as text
Run Code Online (Sandbox Code Playgroud)

我想将上面的值传递为

select * 
from mytable 
where numeric_column in (select trim('[123, 124]', '[]'));
Run Code Online (Sandbox Code Playgroud)

我能理解,numeric_column是数字。但是内部查询将数据作为文本返回。我无法将内部查询转换为,numeric因为它有一个逗号。如果我想将结果转换为'123', '124',我可以运行以下命令并获得预期结果:

select * 
from mytable 
where numeric_column::text in (
    select func_to_change(select trim('[123, 124]', '[]'))
);
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?什么是func_to_change我需要写什么逻辑?

注意:我使用的是 Postgresql 9.1。

a_h*_*ame 5

删除后[]您可以使用string_to_array()将列表转换为整数数组。这可以直接在where子句中使用:

select * 
from mytable 
where numeric_column = ANY(string_to_array(trim('[123, 124]', '[]'),',')::numeric[])
Run Code Online (Sandbox Code Playgroud)