Heim >Backend-Entwicklung >PHP-Tutorial >【原创】MySQL5.7 JSON类型使用介绍_PHP教程
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>