首页 > 解决方案 > 查询对象属性(作为列中的数据) Oracle SQL

问题描述

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

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

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

数据集来自这里

标签: oracleobjectoracle-sqldeveloper

解决方案


如果您使用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

推荐阅读