具有Oracle 12cR2 json功能的查询json数组
我正在使用Oracle(12cR2)json,但找不到查询包含某些元素的数组的方法。以下是测试代码:具有Oracle 12cR2 json功能的查询json数组,json,oracle,Json,Oracle,我正在使用Oracle(12cR2)json,但找不到查询包含某些元素的数组的方法。以下是测试代码: CREATE TABLE json_array ( id NUMBER NOT NULL, array CLOB, CONSTRAINT json_array_pk PRIMARY KEY (id), CONSTRAINT json_array_chk_1 CHECK (array IS JSON) ); INSERT INTO json_array (id, array
CREATE TABLE json_array (
id NUMBER NOT NULL,
array CLOB,
CONSTRAINT json_array_pk PRIMARY KEY (id),
CONSTRAINT json_array_chk_1 CHECK (array IS JSON)
);
INSERT INTO json_array (id, array) VALUES (1, '{"a":[1, 3]}');
INSERT INTO json_array (id, array) VALUES (2, '{"a":[2, 4, 6]}');
INSERT INTO json_array (id, array) VALUES (3, '{"a":[1, 2, 5]}');
INSERT INTO json_array (id, array) VALUES (4, '{"a":[2, 5]}');
INSERT INTO json_array (id, array) VALUES (5, '{"a":[5]}');
INSERT INTO json_array (id, array) VALUES (6, '{"a":[5, 2]}');
COMMIT;
创建域索引:
CREATE SEARCH INDEX idx_json_array ON json_array (array) FOR JSON;
我想查找包含数组元素2和5的所有行,而不考虑它们在数组中的顺序,即SQL应该返回id为3、4、6的行
我尝试了很多选择:
SQL1:
select * from json_array j -- return any arrays containing 2
where json_exists(j.array, '$?(@.a[0] == 2)');
==>返回包含2:id=2、3、4、6的行
SQL2:
==>返回包含2或5的行:id=2、4、5、6
SQL3:
==>不返回行
SQL4:
==>返回包含2或5的行:id=2、3、4、5、6
SQL5:
==>返回包含2和5的行,其中2在5之前
返回所需内容的唯一SQL是:
SQL6:
==>返回id=3、4、6
但是,当元素数量增加时,此解决方案可能会非常麻烦
问:SQL6是否有更好的方法返回相同的结果
用于测试12c R2的Oracle版本
提前谢谢
詹姆斯这有帮助吗
SQL> with MY_TABLE as
2 (
3 select 1 as ID, '{"a":[1, 3]}' as ARRAY
4 from DUAL
5 union all
6 select 2 as ID, '{"a":[2, 4, 6]}' as ARRAY
7 from DUAL
8 union all
9 select 3 as ID, '{"a":[1, 2, 5]}' as ARRAY
10 from DUAL
11 union all
12 select 4 as ID, '{"a":[2, 5]}' as ARRAY
13 from DUAL
14 union all
15 select 5 as ID, '{"a":[5]}' as ARRAY
16 from DUAL
17 union all
18 select 6 as ID, '{"a":[5, 2]}' as ARRAY
19 from DUAL
20 )
21 select ID
22 from MY_TABLE
23 where json_exists(ARRAY,'$?(@.a == 2 && @.a == 5)')
24 /
ID
----------
3
4
6
SQL>
这回答了我的问题。谢谢(很抱歉,我找不到将此答案标记为解决方案的方法)。
select * from json_array j -- return arrays containing 2 at index 1
where json_exists(j.array, '$?(@.a[0] == 2 && @.a[0] == 5)');
select * from json_array j -- returns arrays containing 2 OR 5
where json_textcontains(j.array, '$.a', '[2,5]');
select * from json_array j
where json_textcontains(j.array, '$.a', '{[2] & [5]}');
select * from json_array j
where json_textcontains(j.array, '$.a', '[2]') AND json_textcontains(j.array, '$.a', '[5]');
SQL> with MY_TABLE as
2 (
3 select 1 as ID, '{"a":[1, 3]}' as ARRAY
4 from DUAL
5 union all
6 select 2 as ID, '{"a":[2, 4, 6]}' as ARRAY
7 from DUAL
8 union all
9 select 3 as ID, '{"a":[1, 2, 5]}' as ARRAY
10 from DUAL
11 union all
12 select 4 as ID, '{"a":[2, 5]}' as ARRAY
13 from DUAL
14 union all
15 select 5 as ID, '{"a":[5]}' as ARRAY
16 from DUAL
17 union all
18 select 6 as ID, '{"a":[5, 2]}' as ARRAY
19 from DUAL
20 )
21 select ID
22 from MY_TABLE
23 where json_exists(ARRAY,'$?(@.a == 2 && @.a == 5)')
24 /
ID
----------
3
4
6
SQL>