在内联数组上使用postgresql模糊算子
我想利用postgresql模糊匹配来匹配并非来自表的数据,最终目标是尝试将国家名称与拼写匹配。我试过这个:在内联数组上使用postgresql模糊算子,postgresql,Postgresql,我想利用postgresql模糊匹配来匹配并非来自表的数据,最终目标是尝试将国家名称与拼写匹配。我试过这个: select foo from (select ARRAY['France', 'Thailand', 'Germany' ]) as foo where 'Thailande' % any(foo); 这会产生错误: ERROR: op ANY/ALL (array) requires array on right side 尝试typecastfoo似乎意味着子选择语句的结果不
select foo from (select ARRAY['France', 'Thailand', 'Germany' ]) as foo where 'Thailande' % any(foo);
这会产生错误:
ERROR: op ANY/ALL (array) requires array on right side
尝试typecastfoo
似乎意味着子选择语句的结果不被视为类型数组,而是记录:
=> select foo from (select ARRAY['France', 'Thailand', 'Germany' ]) as foo where 'Thailande' % any(foo::varchar[]);
ERROR: cannot cast type record to character varying[]
LINE 1: ...', 'Germany' ]) as foo where 'Thailande' % any(foo::varchar[...
^
是否有任何方法可以强制使用数组类型或以其他方式实现所需的结果
注意:我使用的是postgresql-9.3.5我认为这里的要点是
foo
不指定字段,而是指定一行
可能你想要这个:
select foo from (select ARRAY['France', 'Thailand', 'Germany' ]) as a(foo)
where 'Thailande' % any(a.foo);
但是请注意,这将选择整个阵列,而不是匹配Thailande
的单个组件
要获取单个单词,我不希望首先使用数组,而是使用VALUES
子句,如:
select foo from (values ('France'), ('Thailand'), ('Germany'))
as a(foo)
where 'Thailande' % a.foo;
结果:
foo
----------
Thailand
谢谢Daniel,这正是我所需要的:)嗨,作为参考,我遇到了这个替代语法(没有添加任何内容,但值得一提):
从unnest中选择foo(数组['France'、'Thailand'、'Germany'])作为foo,其中'Thailand'=foo代码>(据我所知,它实际上是将数组转换为行,因此这与编写值列表是一样的,如果不是因为进行转换而降低了一点效率的话,但我喜欢它的语法)