在 Postgresql 中使用一列数组作为条件

问题描述 投票:0回答:1

我有一个由数组组成的列。

'{"1", "2", "3"}' | '{"4", "5", "6"}' | '{"9", "8", "7"}'

我尝试了

ANY
IN
的混合,但收到此错误消息。

这是我的脚本:

select
*
from tax_role
where tax_role_2021.address_num IN ANY
      (SELECT string_to_array(corporate_data.address_num, '-') FROM corporate_data 
       where lower(corporate_data.name) LIKE '%some_name%'); -- this is the query that return the arrays

但是好像不起作用。

sql postgresql where-clause
1个回答
0
投票

您遇到的错误是由于同时滥用

IN
ANY
子句造成的。

在 PostgreSQL 中,当您与数组进行比较时,可以使用

ANY
关键字,但它不与
IN
一起使用。相反,您可以像这样使用它:

尝试这个修改后的脚本:

SELECT *
FROM tax_role
WHERE tax_role_2021.address_num = ANY
      (SELECT string_to_array(corporate_data.address_num, '-') 
       FROM corporate_data 
       WHERE lower(corporate_data.name) LIKE '%some_name%');

此脚本会将

address_num
中的每个
tax_role_2021
与子查询返回的数组进行比较。如果存在匹配,则来自
tax_role
的行将包含在结果集中。

© www.soinside.com 2019 - 2024. All rights reserved.