Postgres – 如何检查一个空的数组
我正在使用Postgres,我试图写一个像这样的查询:
select count(*) from table where datasets = ARRAY[]
即我想知道有多less行有一个特定列的空数组,但postgres不喜欢:
select count(*) from super_eds where datasets = ARRAY[]; ERROR: syntax error at or near "]" LINE 1: select count(*) from super_eds where datasets = ARRAY[]; ^
语法应该是:
SELECT COUNT(*) FROM table WHERE datasets = '{}'
你用引号加大括号来显示数组文字。
你可以使用array_upper和array_lower函数的事实,在空数组上返回null,所以你可以:
select count(*) from table where array_upper(datasets, 1) is null;
解决scheme查询
select * from table where array_column = ARRAY[NULL]::array_datatype;
例:
table_emp:
id (int)| name (character varying) | (employee_id) (uuid[]) 1 | john doe | {4f1fabcd-aaaa-bbbb-cccc-f701cebfabcd, 2345a3e3-xxxx-yyyy-zzzz-f69d6e2edddd } 2 | jane doe | {NULL} select * from tab_emp where employee_id = ARRAY[NULL]::uuid[]; ------- 2 | jane doe | {NULL}
SELECT COUNT(*) FROM table WHERE datasets = ARRAY(SELECT 1 WHERE FALSE)