mysql - 我如何在我的 sql 表中读取这种类型的 json 对象
问题描述
我在 mysql 表中有这种类型的 json 数据。我怎样才能读到它们
[
{
"price": "1000",
"itemcode": "1",
"itemname": "Break Pads",
"quantity": "1"
},
{
"price": "800",
"itemcode": "3",
"itemname": "Break Oil",
"quantity": "1"
}
]
解决方案
在 MySQL 5.7 中,whereJSON_TABLE()
不可用,典型的解决方案是使用数字表。
select
d.*,
json_unquote(json_extract(
d.items,
concat('$[', n.i, '].price')
)) price,
json_unquote(json_extract(
d.items,
concat('$[', n.i, '].itemcode')
)) itemcode,
json_unquote(json_extract(
d.items,
concat('$[', n.i, '].itemname')
)) itemname,
json_unquote(json_extract(
d.items,
concat('$[', n.i, '].quantity')
)) quantity
from deals d
inner join (
select 0 i
union all select 1
union all select 2
union all select 3
) n
on n.i < json_length(d.items)
这将处理每个数组最多 4 个对象。如果需要更多,可以使用 more 扩展子查询union all
。
推荐阅读
- python-3.x - 卡在进程 sock.connect(sa) 或建立连接
- android - TableLayout 中的 TableRow 和 RecyclerView 中的 TableRow 未对齐
- apache - 设置 Apache 代理以将页面从 App 端口 40120 重定向到 Apache web 80
- java - 为 HTTP 自动化软件创建代理池系统的最佳方法
- google-cloud-platform - gcp 监控“违反时间序列的数量”
- abstract-syntax-tree - 如何将枚举类型与 clang-query 匹配
- postgresql - Postgres 逻辑复制 - 在不访问发布服务器的情况下监控订阅服务器
- android - Nearby Connections API 和 Nearby Share 多次发现同一设备
- python - AttributeError:模块“cv2.cv2”没有属性“TrackerMOSSE_create”
- svn - 慢速数据速率 SVN 结帐以共享文件