야생토끼 블로그

[postgreSQL] text array 형 where 절 조건 본문

Computer science/postgreSQL

[postgreSQL] text array 형 where 절 조건

lazy. 2021. 6. 6. 23:52

postgreSQL arrays 타입이 있고

https://www.postgresql.org/docs/9.5/arrays.html

 

Arrays

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, or composite type can be created. Arrays of domains are not yet supported. To illustrate the use of

www.postgresql.org

 

이중 text[] 타입의 쿼리 사용하게되었다.

 

 8.15.5 Searching in Arrays 부분에서

text array 값들 중에서 10000인 값이 포함된지 찾는 방법은 아래와 같은 쿼리도 있지만

SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR pay_by_quarter[2] = 10000 OR pay_by_quarter[3] = 10000 OR pay_by_quarter[4] = 10000;

이 같은 방식으로도 가능하다.

SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);

또 text array 값 모두가 10000인 값을 찾을 때는 아래의 쿼리

SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);

주어진 array와 하나라도 겹치는 지 검색은 아래와 같이 가능하다

SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY [10000];

아래와 같은 함수도 추후 사용할 수 있을 것 같다.

SELECT array_prepend (1, ARRAY [2,3]); // {1,2,3} 
SELECT array_append (ARRAY [1,2], 3); // {1,2,3} 
SELECT array_cat (ARRAY [1,2], ARRAY [3,4]);  //{1,2,3,4} 
cardinality(schedule) // return the total numberr of elements
array_length(schedule, 1) //return the length of a specified array dimension
SELECT array_dims(ARRAY[1,2] || 3); //return a text representation of array's dimensions.

Comments