postgres将不同的字符与string_to_array中的数组进行比较

时间:2015-10-28 11:48:47

标签: arrays postgresql comparison

我在比较Postgres类型方面遇到了问题,并希望得到一些帮助。我从包含tilda分隔字符串的配置表中提取有效的文档类型,如下所示:

SELECT string_to_array(value,'|') as document_kinds
  FROM company_configs
  WHERE option = 'document_kinds'

这给了我一系列值,所以

' DOC1 | DOC2 | doc3的'成为{doc1,doc2,doc3}

接下来,我需要选择符合我的文档类型的给定人员的文档:

SELECT * FROM people
  JOIN documents ON ...
  WHERE kind IN
   (SELECT string_to_array(value,'|') as document_kinds
    FROM company_configs
    WHERE option = 'document_kinds')

documents.kind列是'字符变化' 我的理解是string_to_array正在生成一个文本值数组' text []'

此查询产生错误' ERROR:运算符不存在:字符变化= text []'

如果我演员出类拔萃的'进入文本,

SELECT * FROM people
 JOIN documents ON ...
 WHERE kind::text IN
  (SELECT string_to_array(value,'|') as visa_document_kinds FROM languages_united.company_configs WHERE option = 'visa_document_kinds')

我收到错误' ERROR:运算符不存在:text = text []'

我不确定如何比较这两者,并对任何建议表示感谢。

提前致谢 丹

Postgres 9.4.1

1 个答案:

答案 0 :(得分:1)

如果您的子查询只返回一行,则可以使用ANY operator选择任何数组元素:

SELECT *
FROM people
JOIN documents ON ...
WHERE kind = ANY (
  SELECT string_to_array(value,'|') as document_kinds
  FROM company_configs
  WHERE option = 'document_kinds');

如果子查询可能返回多行,则可以使用regexp_split_to_table() function

SELECT *
FROM people
JOIN documents ON ...
JOIN (
  SELECT document_kinds
  FROM company_configs,
       regexp_split_to_table(value, '\|') as document_kinds
  WHERE option = 'document_kinds') sub ON sub.document_kinds = kind;

(你必须调整它以匹配你的查询的其余部分。)