postgresql ... WHERE ['ID_two','ID_four'] COINTAINS_AT_LEAST_ONE_ID_FROM ['ID_one','ID_two','ID_three']

ktca8awb  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(135)

我想有一个SQL查询,我检查是否有一个ID(与ID的数组)匹配至少一个ID的其他ID的数组。身份证是刺
应该是这样的:

SELECT * FROM table1 WHERE table1.ids COINTAINS_AT_LEAST_ONE_ID_FROM ['ID_one','ID_two','ID_three']

table1.ids还包含一个带ID的数组。例如,让我们说['ID_two','ID_four']
我看到了几个解决方案,但我使用的是旧的PostgreSQL版本(Redshift上的8.0.2),到目前为止没有任何效果。
你知道解决办法吗?:)
欢迎光临

vql8enpb

vql8enpb1#

这里有一个丑陋的解决方案(如果你绝望了),它应该在8.0上工作:
首先创建t10表,用作迭代器(将其调整为最大数组大小):

CREATE TABLE T10 (ID INTEGER);

INSERT INTO T10 VALUES (1);
INSERT INTO T10 VALUES (2);
INSERT INTO T10 VALUES (3);
INSERT INTO T10 VALUES (4);
INSERT INTO T10 VALUES (5);
INSERT INTO T10 VALUES (6);
INSERT INTO T10 VALUES (7);
INSERT INTO T10 VALUES (8);
INSERT INTO T10 VALUES (9);
INSERT INTO T10 VALUES (10);

下面是测试表:

create table table1 (
    ids text[]
);

insert into table1 (ids) values (ARRAY['ID_two','ID_four']);

select * from table1;

       ids        
------------------
 {ID_two,ID_four}

然后,查询,使用T10迭代数组的所有成员:

\set myids ARRAY['''ID_one''','''ID_two''','''ID_three''']

select *
  from (
     select ids[iter.pos] as ids
     from table1
     join (select id as pos from t10) iter
     on iter.pos <= array_length(ids,1)) as A
  join (
     select (:myids)[iter.pos] as ids
     from (select id as pos from t10) iter
     where iter.pos <= array_length(:myids,1)) as B
  on a.ids = b.ids;

  ids   |  ids
--------+--------
 ID_two | ID_two

相关问题