Table of Contents
【原创】MySQL5.7 JSON类型使用介绍
Home php教程 php手册 【原创】MySQL5.7 JSON类型使用介绍

【原创】MySQL5.7 JSON类型使用介绍

Jun 13, 2016 am 08:51 AM
android

【原创】MySQL5.7 JSON类型使用介绍

JSON是一种轻量级的数据交换格式,采用了独立于语言的文本格式,类似XML,但是比XML简单,易读并且易编写。对机器来说易于解析和生成,并且会减少网络带宽的传输。
JSON的格式非常简单:名称/键值。之前MySQL版本里面要实现这样的存储,要么用VARCHAR要么用TEXT大文本。 MySQL5.7发布后,专门设计了JSON数据类型以及关于这种类型的检索以及其他函数解析。 我们先看看MySQL老版本的JSON存取。


示例表结构:

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>CREATE TABLE json_test(<br /></li><li>id INT,<br /></li><li>person_desc TEXT<br /></li><li>)ENGINE INNODB;</li></ol>
Copy after login




我们来插入一条记录:


<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>INSERT INTO json_test VALUES (1,'{<br /></li><li>"programmers": [{<br /></li><li>"firstName": "Brett",<br /></li><li>"lastName": "McLaughlin",<br /></li><li>"email": "aaaa"<br /></li><li>}, {<br /></li><li>"firstName": "Jason",<br /></li><li>"lastName": "Hunter",<br /></li><li>"email": "bbbb"<br /></li><li>}, {<br /></li><li>"firstName": "Elliotte",<br /></li><li>"lastName": "Harold",<br /></li><li>"email": "cccc"<br /></li><li>}],<br /></li><li>"authors": [{<br /></li><li>"firstName": "Isaac",<br /></li><li>"lastName": "Asimov",<br /></li><li>"genre": "sciencefiction"<br /></li><li>}, {<br /></li><li>"firstName": "Tad",<br /></li><li>"lastName": "Williams",<br /></li><li>"genre": "fantasy"<br /></li><li>}, {<br /></li><li>"firstName": "Frank",<br /></li><li>"lastName": "Peretti",<br /></li><li>"genre": "christianfiction"<br /></li><li>}],<br /></li><li>"musicians": [{<br /></li><li>"firstName": "Eric",<br /></li><li>"lastName": "Clapton",<br /></li><li>"instrument": "guitar"<br /></li><li>}, {<br /></li><li>"firstName": "Sergei",<br /></li><li>"lastName": "Rachmaninoff",<br /></li><li>"instrument": "piano"<br /></li><li>}]<br /></li><li>}');</li></ol>
Copy after login



那一般我们遇到这样来存储JSON格式的话,只能把这条记录取出来交个应用程序,有应用程序来解析。


现在到了MySQL5.7,我们重新修改下表结构:



<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>ALTER TABLE json_test MODIFY person_desc json;</li></ol>
Copy after login


先看看插入的这行JSON数据有哪些KEY:

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G<br /></li><li>*************************** 1. row ***************************<br /></li><li>id: 1<br /></li><li>keys: ["authors", "musicians", "programmers"]<br /></li><li>1 row in set (0.00 sec)</li></ol>
Copy after login




我们可以看到,里面有三个KEY,分别为authors,musicians,programmers。那现在找一个KEY把对应的值拿出来:

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM<br /></li><li>-> (<br /></li><li>-> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test<br /></li><li>-> UNION ALL<br /></li><li>-> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test<br /></li><li>-> UNION ALL<br /></li><li>-> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test<br /></li><li>-> ) AS T1<br /></li><li>-> ORDER BY NAME DESC\G<br /></li><li>*************************** 1. row ***************************<br /></li><li>name: "Williams"<br /></li><li>AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"}<br /></li><li>*************************** 2. row ***************************<br /></li><li>name: "Peretti"<br /></li><li>AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"}<br /></li><li>*************************** 3. row ***************************<br /></li><li>name: "Asimov"<br /></li><li>AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"}<br /></li><li><br /></li><li><br /></li><li>3 rows in set (0.00 sec)</li></ol>
Copy after login


现在来把详细的值罗列出来:

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mysql> SELECT<br /></li><li>-> json_extract(AUTHORS,'$.firstName[0]') AS "firstname",<br /></li><li>-> json_extract(AUTHORS,'$.lastName[0]') AS "lastname",<br /></li><li>-> json_extract(AUTHORS,'$.genre[0]') AS "genre"<br /></li><li>-> FROM<br /></li><li>-> (<br /></li><li>-> SELECT id,json_extract(person_desc,'$.authors[0]') AS "authors" FROM json<br /></li><li>_test<br /></li><li>-> ) AS T\G<br /></li><li>*************************** 1. row ***************************<br /></li><li>firstname: "Isaac"<br /></li><li>lastname: "Asimov"<br /></li><li>genre: "sciencefiction"<br /></li><li>1 row in set (0.00 sec)</li></ol>
Copy after login


我们进一步来演示把authors 这个KEY对应的所有对象删掉。


<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mysql> UPDATE json_test<br /></li><li>-> SET person_desc = json_remove(person_desc,'$.authors')\G<br /></li><li>Query OK, 1 row affected (0.01 sec)<br /></li><li>Rows matched: 1  Changed: 1  Warnings: 0</li></ol>
Copy after login


查找下对应的KEY,发现已经被删除掉了。

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist<br /></li><li>s FROM json_test\G<br /></li><li>*************************** 1. row ***************************<br /></li><li>authors_exists: 0<br /></li><li>1 row in set (0.00 sec)</li></ol>
Copy after login


总结下, 虽然MySQL5.7 开始支持JSON数据类型,但是我建议如果要使用的话,最好是把这样的值取出来,然后在应用程序段来计算,毕竟数据库是用来处理简单数据的。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T

See all articles