代码之家  ›  专栏  ›  技术社区  ›  Agenobarb

PostgreSQL:处理数组

  •  0
  • Agenobarb  · 技术社区  · 6 年前

    我在PostgreSQL表中有一个可用的列大小,它有一个类型数组:text[]

    select available_sizes from products;
    
     {37,38,39,40}
    ...
    

    有时我需要检查哪些行包含某些值,例如39和40,所以我尝试这样做:

    select * 
    from products 
    where available_sizes && ('{39, 40}');
    

    返回包含39或40的行

    select * 
    from products 
    where available_sizes = ANY ('{41, 42}');
    

    返回错误:“找不到数据类型text[]的数组类型”

    你怎么解决这个问题?对不起,我不是SQL/PostgreSQL方面的专家

    1 回复  |  直到 6 年前
        1
  •  1
  •   a_horse_with_no_name    6 年前

    && 是“重叠”运算符 described as “有共同点”

    @>

    select * 
    from products 
    where available_sizes @> ('{39, 40}');