0

我有一列类型为 int2[] 数组。我想对此列进行 IN 查询。

我在谷歌上搜索它,发现 @> 运算符在数组列中搜索,但它似乎不适用于 int2 类型的数组列。但它适用于 int[]

有谁知道为什么它在 int2[] 上不起作用?

ecabuk=# CREATE TABLE "Test"("Column1" int2[]);
CREATE TABLE

ecabuk=# INSERT INTO "Test" VALUES ('{10, 15, 20}');
INSERT 0 1

ecabuk=# INSERT INTO "Test" VALUES ('{10, 20, 30}');
INSERT 0 1

ecabuk=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20];
ERROR:  operator does not exist: smallint[] @> integer[]
LINE 1: ...LAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

ecabuk=# ALTER TABLE "Test" ALTER COLUMN "Column1" type int[];
ALTER TABLE

ecabuk=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20];
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.200..0.204 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::integer[])
 Total runtime: 0.256 ms
(3 rows)

ecabuk=# 
4

2 回答 2

1

当您指定 ARRAY[20] 时,它会将其视为 int[] 而不是 int2[]。合适的演员表会让它表现得很好。

test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2];
ERROR:  operator does not exist: smallint[] @> integer[]
LINE 1: ...LAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20::int2];
                                            QUERY PLAN

--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.060..0.064 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::smallint[])
 Total runtime: 0.116 ms
(3 rows)


test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20]::int2[];
                                            QUERY PLAN

--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.028..0.032 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::smallint[])
 Total runtime: 0.080 ms
(3 rows)
于 2013-02-19T21:43:16.417 回答
0

在这里工作。恐怕你需要提供一个失败的例子:

=> SELECT ARRAY[1,2,3]::int2[] @> ARRAY[1,2]::int2[];
 ?column? 
----------
   t
  (1 row)

=> SELECT ARRAY[1,2,3]::int2[] @> ARRAY[3,4]::int2[];
 ?column? 
----------
  f
 (1 row)

如果它不起作用,那会很奇怪,因为它是在 anyarray 上定义的:

=> \do '@>'
                               List of operators

   Schema   | Name | Left arg type | Right arg type | Result type | Description 
------------+------+---------------+----------------+-------------+-------------
 pg_catalog | @>   | aclitem[]     | aclitem        | boolean     | contains
 pg_catalog | @>   | anyarray      | anyarray       | boolean     | contains
 ...

在 9.1 和 9.2 中测试

于 2013-02-19T20:04:47.543 回答