如何在ARRAY类型列中为NULL值编写WHERE子句?

时间:2019-06-21 10:55:25

标签: sql postgresql

我创建了一个表,其中包含一列字符串ARRAY,类型为:

CREATE TABLE test
(
    id integer NOT NULL,
    list text[] COLLATE pg_catalog."default",
    CONSTRAINT test_pkey PRIMARY KEY (id)
)

然后我添加了包含该数组各种值的行,包括一个空数组和丢失的数据(null):

insert into test (id, list) values (1, array['one', 'two', 'three']);
insert into test (id, list) values (2, array['four']);
insert into test (id, list) values (3, array['']);
insert into test (id, list) values (4, array[]::text[]); // empty array
insert into test (id, list) values (5, null); // missing value

pgAdmin显示如下表:

Table in pgAdmin

我正在尝试在[null]列中获取包含空值(list)的行,但是:

select * from test where list = null;

...不返回任何行,并且:

select * from test where list = '{}'; 

...返回带有id = 4的行。

如何编写将ARRAY类型的列作为NULL值的WHERE子句?

3 个答案:

答案 0 :(得分:0)

demo:db<>fiddle

... WHERE list IS NULL

答案 1 :(得分:0)

赞:

select * from test where list IS NULL;

答案 2 :(得分:0)

select * from test where list IS null;