oracle - 查询对象属性(作为列中的数据) Oracle SQL
问题描述
我导入了一个数据集,该数据集在 GENRES 列中有一个对象,我想知道是否有一种(初学者的)方法来查询每个条目中的数据。
[{“id”:12,“名称”:“冒险”},{“id”:10751,“名称”:“家庭”},{“id”:14,“名称”:“幻想”}]
例如,如果我想查询所有具有“id”的电影:12。
数据集来自这里
解决方案
如果您使用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
推荐阅读
- python - 打印出在 python3 执行期间调用的函数的简单、pythonic 方式
- firebase - 将 HttpsCallable 与服务凭据一起使用
- java - REST API 中可以强制查询参数吗
- validation - 当我们根据训练步骤进行训练时,何时进行验证?
- r - 如何使用 dplyr group_by 并获取每个不同分组的索引?
- python-3.x - 如何在 py3 代码中将列表转换为 scapy.plist.PacketList?
- recursion - 不能接受 2 的 CAR(Common Lisp)
- mysql - 错误:1452 - 添加触发器后插入操作失败
- nginx - Kong 2.0.2 服务 api 和静态内容
- javascript - 如何切换触发器