PostgreSQL,unnest array into values

PostgreSQL, unnest array into values

假设我有文本输入:

周六购买,XXX 送货,地址 YYY,付款方式 ZZZ

请向我们提供价格和库存情况

К52-1Б - 68мкф/50в
К52-1 - 100мкф/63в

如何修改这个查询,这样我就不需要手动拆分行了?

with long_string (ls) as (values
    ('Buying on Saturday, delivery by XXX, address YYY, payment method ZZZ'),
    ('Please quote us on prices and stock availability'),
    ('К52-1Б - 68мкф/50в'),
    ('К52-1 - 100мкф/63в')
)
select products.*, matches, similarity
from products
cross join long_string
cross join lateral
(select
    (name % ls)::int as matches,
    similarity(name, ls) as similarity
) m
where matches > 0 and similarity > 0.7
order by matches desc, similarity desc;

我试过了

with long_string (ls) as (
    values
        unnest(string_to_array('Buying on Saturday, delivery by XXX, address YYY, payment method ZZZ 
        Please quote us on prices and stock availability
        К52-1Б - 68мкф/50в
        К52-1 - 100мкф/63в', '\n'))
    )
select products.*, matches, similarity
....

但是有错误ERROR: syntax error at or near "unnest"

如果这只是解析字符串,那么:

with long_string (ls) as (
    select unnest(string_to_array(str, E'\n'))
    from (values ('Buying on Saturday, delivery by XXX, address YYY, payment method ZZZ 
Please quote us on prices and stock availability
К52-1Б - 68мкф/50в
К52-1 - 100мкф/63в')
         ) v(str)
    )
select ls
from long_string;

Here 是一个 db<>fiddle.

或者您可以这样做:

with long_string (ls) as (
    select
         trim(regexp_split_to_table('Buying on Saturday, delivery by XXX, address YYY, payment method ZZZ
        Please quote us on prices and stock availability
        К52-1Б - 68мкф/50в
        К52-1 - 100мкф/63в', '\n'))
    )
select *
from long_string