【问题标题】:query json array with Oracle 12cR2 json features使用 Oracle 12cR2 json 特性查询 json 数组
【发布时间】:2017-06-10 10:56:05
【问题描述】:

我正在使用 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) 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:

select * from json_array j  -- return arrays containing 2 at index 1
where json_exists(j.array, '$?(@.a[0] == 2  || @.a[0] == 5)');

==> 返回包含 2 或 5 的行:id = 2, 4, 5, 6

SQL3:

select * from json_array j  -- return arrays containing 2 at index 1
where json_exists(j.array, '$?(@.a[0] == 2  && @.a[0] == 5)');

==> 不返回任何行

SQL4:

select * from json_array j  -- returns arrays containing 2 OR 5
where json_textcontains(j.array, '$.a', '[2,5]');

==> 返回包含 2 或 5 的行:id = 2, 3, 4, 5, 6

SQL5:

select * from json_array j  
where json_textcontains(j.array, '$.a', '{[2] & [5]}');

==> 返回包含 2 AND 5 的行,其中 2 在 5 之前

返回我想要的唯一 SQL 是: SQL6:

select * from json_array j
where json_textcontains(j.array, '$.a', '[2]') AND json_textcontains(j.array, '$.a', '[5]');

==> 返回 id = 3, 4, 6

但是当元素数量增加时,这种解决方案会非常麻烦。

问题:SQL6 是否有更好的选择来返回相同的结果?

用于测试 12c R2 的 Oracle 版本

提前致谢

詹姆斯

【问题讨论】:

    标签: json oracle


    【解决方案1】:

    这有帮助吗...

       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>
    

    【讨论】:

    • 这回答了我的问题。谢谢! (抱歉,我找不到将这个答案标记为解决方案的方法)。
    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多