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

postgres comparing varying character with array from string_to_array

我在比较 Postgres 类型时遇到问题,如果能提供一些帮助,我将不胜感激。我正在从包含波浪号分隔字符串的配置 table 中提取有效文档类型,如下所示:

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

这给了我一组值,所以

'doc1|doc2|doc3' 变成 {doc1,doc2,doc3}

接下来我需要 select 与我的文件类型匹配的给定人员的文件:

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 列是 'character varying' 我的理解是 string_to_array 正在生成一个文本值数组 'text[]'

此查询产生错误 'ERROR: operator does not exist: character varying = text[]'

如果我将 'kind' 转换为文本,使用

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: operator does not exist: text = text[]'

我不确定如何比较两者,如有任何建议,我将不胜感激。

提前致谢 旦

Postgres 9.4.1

如果您的子查询 returns 恰好一行:

,您可以使用 ANY operator 针对任何数组元素 select
SELECT *
FROM people
JOIN documents ON ...
WHERE kind = ANY (
  SELECT string_to_array(value,'|') as document_kinds
  FROM company_configs
  WHERE option = 'document_kinds');

如果子查询可能returns多行,可以使用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;

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