Program Club

Postgres-빈 배열을 확인하는 방법

proclub 2020. 12. 30. 08:22
반응형

Postgres-빈 배열을 확인하는 방법


Postgres를 사용하고 있으며 다음과 같은 쿼리를 작성하려고합니다.

select count(*) from table where datasets = ARRAY[]

즉, 특정 열에 대해 빈 배열이있는 행 수를 알고 싶지만 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;

솔루션 쿼리 :
select id, name, employee_id 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 id, name, employee_id from tab_emp where employee_id = ARRAY[NULL]::uuid[];

    -------
2       | jane doe                 | {NULL}

SELECT  COUNT(*)
FROM    table
WHERE   datasets = ARRAY(SELECT 1 WHERE FALSE)

참조 URL : https://stackoverflow.com/questions/737669/postgres-how-to-check-for-an-empty-array

반응형