【问题标题】:Postgres array lookup multiple values in where clausePostgres数组在where子句中查找多个值
【发布时间】:2021-06-03 10:17:17
【问题描述】:

我有下表的数据

create table stud(key int, person text, subject_id int[]);

insert into stud select 1,'Alex',array[2,7,9];
insert into stud select 2,'Peter',array[4,9,12];
insert into stud select 3,'Tokaver',array[8];
insert into stud select 4,'Machel',array[11,15];

表格外观

我可以在类似的地方过滤单个subject_id

select * from stud where 9=any(subject_id)

我们如何在 where 子句中过滤多个 subject_id

select * from stud where (8,9) in any(subject_id)

【问题讨论】:

    标签: sql arrays postgresql where-clause


    【解决方案1】:

    demo:db<>fiddle

    您可以对数组使用重叠运算符&amp;&amp;

    select * from stud 
    where array[8,9] && subject_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2020-09-18
      • 2017-11-11
      • 2021-06-23
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多