无法在嵌套查询的结果上添加 WHERE 约束 - postgres 中的错误 42883

Fail to add WHERE constraint on result of nested query - ERROR 42883 in postgres

这对我来说似乎是一个非常直接的嵌套查询 - 但显然我完全遗漏了一些东西。

设置:

鉴于表格很大,我使用一些先验知识来减少要匹配的公司编号的数量:

加上

所以:

SELECT *
INTO ccod_psc
FROM psc
WHERE (SELECT DISTINCT psc.company_number)
          in
      (SELECT DISTINCT stacked_company_numbers
       FROM (SELECT company_number_1
             FROM ccod
             WHERE company_number_1 is not null
             UNION ALL
             SELECT company_number_2
             FROM ccod
             WHERE company_number_2 is not null) AS stacked_company_numbers
       WHERE char_length(stacked_company_numbers::text) = 8);
[42883] ERROR: operator does not exist: text = record
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 86

现在这个错误的话已经很不言自明了,但我不知道如何解决它,并且尝试了很多与错误 42883 相关的其他答案,但都没有成功。

感谢您的帮助。

-- 编辑:

我还找到了一种使查询有效的方法:

SELECT *
INTO ccod_psc
FROM psc
WHERE (SELECT DISTINCT psc.company_number)
          in
      (SELECT company_number_1
       FROM ccod
       WHERE company_number_1 is not null
       UNION -- this takes cares of the duplicates.
       SELECT company_number_2
       FROM ccod
       WHERE company_number_2 is not null) AS stacked_company_numbers
WHERE char_length(stacked_company_numbers::text) = 8)

我怀疑 exists 做了你想要的:

select p.*
from psc p
where 
    char_length(p.company_numbers::text) = 8
    and exists (
        select 1
        from ccod c
        where p.company_number in (c.company_number_1, c.company_number_2)
    )

查询从psc中取出记录,其company_number长度为8个字符,可以在table中的company_number_1company_number_2中找到ccod.

我认为两个 left join 效率更高:

SELECT p.*
INTO ccod_psc c 
FROM psc p LEFT JOIN
     ccod c1
     ON p.company_number = c1.company_number_1 LEFT JOIN
     ccod c2
     ON p.company_number = c2.company_number_2 AND
        c1.company_number_1 IS NULL
WHERE c1.company_number_1 IS NOT NULL OR
      c2.company_number_2 IS NOT NULL 

这种方法的优点是可以在 ccod(company_number_1)ccod(company_number_2) 上使用索引。

编辑:

如果 ccod 中可以有重复项,那么 exists 可能会有更好的性能:

SELECT p.*
INTO ccod_psc c 
FROM psc p
WHERE EXISTS (SELECT 1
              FROM ccod c1
              WHERE p.company_number = c1.company_number_1
             ) OR
     EXISTS (SELECT 1
              FROM ccod c2
              WHERE p.company_number = c2.company_number_2
             ) ;