postgresql 데이터 지원 jsonb / json 에서 array 또는 int 형식 으로 교 집합 비교
4655 단어 데이터베이스 학습
표 에 jsonb 필드 형식 이 포함 되 어 있다 면: 필드 에 array 형식의 데이터 나 number 형식의 데이터 가 있 습 니 다.(이 종 류 는 jsonb typeof (parent - > children) 방법 으로 표 시 됩 니 다)
2. 개인 적 으로 주의해 야 할 세부 사항:
내용 주의: 현재 캡 처 된 데 이 터 는 여전히 jsonb 형식 입 니 다.pg 를 통 해type: of () 검 사 를 진행 합 니 다.jsonb 형식 에서 캡 처 한 데 이 터 는 여전히 jsonb 입 니 다. 다른 유형 으로 바 꾸 려 면 특수 처 리 를 해 야 합 니 다.
jsonbtype of () 는 형식 number 가 가 려 졌 음 을 표시 합 니 다.그것 은 비록 현실 은 number 이지 만, 사실은 지금도 여전히 jsonb 이다.
다음은 jsontype: of 의 공식 설명.
3. 교 집합 비 교 를 하려 면 & & 기 호 를 통 해 두 개의 array [] 를 비교 할 수 있다.
4. JSON 에 저 장 된 배열 을 되 돌려 주 는 방법
대부분의 JSON 연산 자 는 TEXT 나 JSON 으로 되 돌아 갑 니 다. (integer [] 형식 으로 되 돌아 가지 않 았 습 니 다.)
예 를 들 어 다음 조회 연산 자 - > 배열 로 돌아 가기
postgres=# select pg_typeof('{"a":[1,2,3],"b":[4,5,6]}'::json->'a'), '{"a":[1,2,3],"b":[4,5,6]}'::json->'a';
pg_typeof | ?column?
-----------+----------
json | [1,2,3]
(1 row)
연산 자 - > > 텍스트 되 돌리 기
postgres=# select pg_typeof('{"a":[1,2,3],"b":[4,5,6]}'::json->>'a'), '{"a":[1,2,3],"b":[4,5,6]}'::json->>'a';
pg_typeof | ?column?
-----------+----------
text | [1,2,3]
(1 row)
하면, 만약, 만약...type: of 또는 jsonbtype: of 는 array 입 니 다. 다음 function 을 통 해 배열 을 줄 로 변환 할 수 있 습 니 다. 어떤 배열 이 든 text 줄 로 되 돌려 줍 니 다.
json_array_elements_text(json)
jsonb_array_elements_text(jsonb)
postgres=# select pg_typeof(col),col from (select json_array_elements_text('{"a":"B","b":[1,2,3,4,5,6]}'::json->'b') col) t;
pg_typeof | col
-----------+-----
text | 1
text | 2
text | 3
text | 4
text | 5
text | 6
(6 rows)
배열 구조 기 를 사용 하면 여러 개의 기록 을 배열 로 구성 할 수 있다.
postgres=# SELECT array(select json_array_elements_text('{"a":"B","b":[1,2,3,4,5,6]}'::json->'b'));
array
---------------
{1,2,3,4,5,6}
(1 row)
postgres=# SELECT pg_typeof(array(select json_array_elements_text('{"a":"B","b":[1,2,3,4,5,6]}'::json->'b')));
pg_typeof
-----------
text[]
(1 row)
何转换JSONB数组的类型
如果想构造int[],在构造前,可以将行的输出转换为对应的格式,
postgres=# SELECT array(select (json_array_elements_text('{"a":"B","b":[1,2,3,4,5,6]}'::json->'b'))::int );
array
---------------
{1,2,3,4,5,6}
(1 row)
postgres=# SELECT pg_typeof(array(select (json_array_elements_text('{"a":"B","b":[1,2,3,4,5,6]}'::json->'b'))::int ));
pg_typeof
-----------
integer[]
(1 row)
JSON 배열 을 작성 하여 SQL 배열 로 변환 하 는 함수
이상 의 방법 을 함수 로 변환 하여 사용 할 수 있 으 며, json type: of = array 의 json 또는 jsonb 대상 을 입력 하여 text 배열 을 출력 할 수 있 습 니 다.jsonb, json 배열 을 텍스트 배열 로 변환
CREATE OR REPLACE FUNCTION json_arr2text_arr(_js jsonb)
RETURNS text[] AS
$func$
SELECT ARRAY(SELECT jsonb_array_elements_text(_js))
$func$
LANGUAGE sql IMMUTABLE;
CREATE OR REPLACE FUNCTION json_arr2text_arr(_js json)
RETURNS text[] AS
$func$
SELECT ARRAY(SELECT json_array_elements_text(_js))
$func$
LANGUAGE sql IMMUTABLE;
jsonb, json array 를 int array 로 변환
CREATE OR REPLACE FUNCTION json_arr2int_arr(_js jsonb)
RETURNS int[] AS
$func$
SELECT ARRAY( SELECT (jsonb_array_elements_text(_js))::int )
$func$
LANGUAGE sql IMMUTABLE;
CREATE OR REPLACE FUNCTION json_arr2int_arr(_js json)
RETURNS int[] AS
$func$
SELECT ARRAY( SELECT (json_array_elements_text(_js))::int )
$func$
LANGUAGE sql IMMUTABLE;
예시
postgres=# select col, pg_typeof(col) from (select json_arr2text_arr(c1->'f') col from t3) t;
col | pg_typeof
-----------+-----------
{1,2,3,4} | text[]
(1 row)
postgres=# select col, pg_typeof(col) from (select json_arr2int_arr(c1->'f') col from t3) t;
col | pg_typeof
-----------+-----------
{1,2,3,4} | integer[]
(1 row)
postgres=# select col, pg_typeof(col) from (select json_arr2text_arr(c1->'g') col from t3) t;
col | pg_typeof
-------+-----------
{a,b} | text[]
(1 row)
hibernate-jpa function, boolean
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
MyBatis 업데이트, 삭제, 삽입UserMapper.java: UserMapper: User 실체 클래스: 도구 종류: 프로필 mybatis: 첨삭 검사: 테스트 클래스: UserMapper 인터페이스 (추가 삭제 수정 은 모두 테스트 클래스 에서...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.