mysql8 json_table_新特性解读 | MySQL 8.0 json到表的转换

本文介绍了MySQL 8.0的新特性——JSON_TABLE函数,该函数能够将JSON数据转换成表格形式。通过示例展示了如何使用JSON_TABLE解析不同层级的JSON对象,将其转换为易于查询的表格结构。
摘要由CSDN通过智能技术生成

原创: 杨涛涛

我们知道,JSON是一种轻量级的数据交互的格式,大部分NO SQL数据库的存储都用JSON。MySQL从5.7开始支持JSON格式的数据存储,并且新增了很多JSON相关函数。MySQL 8.0 又带来了一个新的把JSON转换为TABLE的函数JSON_TABLE,实现了JSON到表的转换。

举例一

我们看下简单的例子:

简单定义一个两级JSON 对象

mysql> set @ytt='{"name":[{"a":"ytt","b":"action"}, {"a":"dble","b":"shard"},{"a":"mysql","b":"oracle"}]}';

Query OK, 0 rows affected (0.00 sec)

第一级:

mysql> select json_keys(@ytt);

+-----------------+

| json_keys(@ytt) |

+-----------------+

| ["name"] |

+-----------------+

1 row in set (0.00 sec)

第二级:

mysql> select json_keys(@ytt,'$.name[0]');

+-----------------------------+

| json_keys(@ytt,'$.name[0]') |

+-----------------------------+

| ["a", "b"] |

+-----------------------------+

1 row in set (0.00 sec)

我们使用MySQL 8.0 的JSON_TABLE 来转换 @ytt。

mysql> select * from json_table(@ytt,'$.name[*]' columns (f1 varchar(10) path '$.a', f2 varchar(10) path '$.b')) as tt;

+-------+

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Window functions: MySQL 8.0 now supports window functions, which allows users to perform complex analytical queries with ease. 2. Common Table Expressions (CTEs): CTEs are temporary result sets that can be used within a single SQL statement. This feature makes it easier to write complex queries and improves code readability. 3. Recursive Common Table Expressions (RCTEs): RCTEs are a special type of CTE that allows users to write recursive queries. This feature is useful for queries that involve hierarchical data. 4. JSON enhancements: MySQL 8.0 includes several enhancements to its JSON support, including better performance and improved functionality. 5. Invisible indexes: This feature allows users to mark an index as "invisible," which means that it will not be used by the query optimizer. This can be useful for testing the impact of removing an index without actually removing it. 6. Instant ADD COLUMN: MySQL 8.0 includes a new feature that allows users to add a new column to a table without copying the entire table. This can be useful for large tables with many columns. 7. Roles: MySQL 8.0 introduces the concept of roles, which allows users to define sets of privileges that can be assigned to multiple users. 8. Improved performance: MySQL 8.0 includes several performance improvements, including better support for multi-threaded queries and faster data loading. 9. Improved security: MySQL 8.0 includes several security enhancements, including improved password management and support for encrypted connections. 10. Improved replication: MySQL 8.0 includes several improvements to its replication capabilities, including support for multiple replication channels and better conflict detection and resolution.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值