【问题标题】:query object attributs (as data in a column) Oracle SQL查询对象属性(作为列中的数据) Oracle SQL
【发布时间】:2021-06-20 17:19:49
【问题描述】:

我导入了一个数据集,该数据集在 GENRES 列中有一个对象,我想知道是否有一种(初学者的)方法来查询每个条目中的数据。

[{“id”:12,“name”:“冒险”},{“id”:10751,“name”:“家庭”},{“id”:14,“name”:“幻想” }]

例如,如果我想查询所有具有“id”的电影:12。

数据集来自here

【问题讨论】:

    标签: oracle object oracle-sqldeveloper


    【解决方案1】:

    如果您使用IS JSON 约束存储数据,则可以使用JSON_TABLE 将JSON 扩展为列。

    --Create a table.
    create table tmdb(movie_id number, genres clob check (genres is json));
    
    --Insert a test row.
    insert into tmdb
    values(1, '[{"id": 12, "name": "Adventure"}, {"id": 10751, "name": "Family"}, {"id": 14, "name": "Fantasy"}]');
    
    --Use JSON_TABLE to convert JSON to relational data.
    select movie_id, id, name
    from tmdb,
        json_table
        (
            genres, '$[*]'
            columns
            (
                id number path '$.id',
                name varchar2(4000) path '$.name'
            )
        ) as jt;
    
    --Results:
    
    MOVIE_ID   ID      NAME
    --------   -----   ---------
           1      12   Adventure
           1   10751   Family
           1      14   Fantasy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2016-07-06
      • 2012-01-22
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      相关资源
      最近更新 更多