Heim  >  Artikel  >  Datenbank  >  So verwenden Sie json_extract in MySQL

So verwenden Sie json_extract in MySQL

WBOY
WBOYnach vorne
2023-05-31 16:58:161715Durchsuche

    1. Vorwort

    mysql5.7-Version beginnt mit der Unterstützung von Feldern vom Typ JSON
    json_extract kann Die vollständige Abkürzung ist ->
    json_unquote(json_extract()) kann vollständig abgekürzt werden als ->>
    Die meisten davon In der folgenden Einführung wird die Abkürzung

    verwendet. 2. Erstellen Sie eine Beispieltabelle

    CREATE TABLE `test_json` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `content` json DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
    # 插入两条测试用的记录
    INSERT INTO `test_json` (`content`) VALUES ('{\"name\":\"tom\",\"age\":18,\"score\":[100,90,87],\"address\":{\"province\":\"湖南\",\"city\":\"长沙\"}}');
    INSERT INTO `test_json` (`content`) VALUES ('[1, "apple", "red", {"age": 18, "name": "tom"}]');
    Inhalt1{"age": 18, "name": „tom“, „score“: [100, 90, 87], „address“: {“city“: „Changsha“, „province“: „Hunan“}}2 [1, „apple“, „red“, {“age“: 18, „name“: „tom“}]#🎜 🎜##🎜🎜 #3. Grundlegende Syntax
    id#🎜🎜 #
    - Rufen Sie den Wert ab, der einem Schlüssel im JSON-Objekt entspricht

    In der Funktion json_extract stellt der erste Parameterinhalt die JSON-Daten dar und der zweite Parameter ist der JSON-Pfad, wobei $ die JSON-Daten selbst darstellt und $.name das Abrufen des Werts mit dem Schlüsselnamen in JSON darstellt
    • Sie können -> Ausdruck anstelle von json_extract verwenden val wird in Anführungszeichen wie „tom“ eingeschlossen. Wenn diese Art von Daten in ein Programmobjekt analysiert wird, kann sie als „tom“ maskiert werden. Um dieses Problem zu lösen, können Sie eine weitere Ebene der Funktion json_unquote einschließen oder ->> anstelle von ->
    • content verwenden :
    • {"age": 18, "name": "tom", "score": [100, 90, 87], "address": {"city": "Changsha", "province": " Hunan" ”}}

      # 得到"tom"
      select json_extract(content,'$.name') from test_json where id = 1;
      # 简写方式:字段名->表达式等价于json_extract(字段名,表达式)
      select content->'$.name' from test_json where id = 1;
      # 结果:
      +--------------------------------+
      | json_extract(content,'$.name') |
      +--------------------------------+
      | "tom"                          |
      +--------------------------------+
      +-------------------+
      | content->'$.name' |
      +-------------------+
      | "tom"             |
      +-------------------+
      
      # 解除双引号,得到tom
      select json_unquote(json_extract(content,'$.name')) from test_json where id = 1;
      # 简写方式:字段名->>表达式等价于json_unquote(json_extract(字段名,表达式))
      select content->>'$.name' from test_json where id = 1;
      # 结果:
      +----------------------------------------------+
      | json_unquote(json_extract(content,'$.name')) |
      +----------------------------------------------+
      | tom                                          |
      +----------------------------------------------+
      +--------------------+
      | content->>'$.name' |
      +--------------------+
      | tom                |
      +--------------------+
    • - Holen Sie sich ein Element im JSON-Array


    In der json_extract-Funktion der erste Parameter Inhalt stellt JSON-Daten dar, der zweite Parameter ist der JSON-Pfad, wobei $ die JSON-Daten selbst darstellt, $[i] das Abrufen des Elements mit Index i im JSON-Array darstellt (Index beginnt bei 0)

    #🎜🎜 #

    Dasselbe wie beim Abrufen des Schlüsselwerts: Wenn das erhaltene Element eine Zeichenfolge ist, ruft die Standardmethode auch die Zeichen in doppelte Anführungszeichen ab, wodurch das Programm maskiert wird. Die Methode verwendet auch json_unquote Funktion, oder verwenden Sie stattdessen ->> ; {"age": 18, " name": „tom“}]

      # 得到"apple"
      select json_extract(content,'$[1]') from test_json where id = 2;
      # 简写,效果同上
      select content->'$[1]' from test_json where id = 2;
      # 结果:
      +------------------------------+
      | json_extract(content,'$[1]') |
      +------------------------------+
      | "apple"                      |
      +------------------------------+
      +-----------------+
      | content->'$[1]' |
      +-----------------+
      | "apple"         |
      +-----------------+
      
      # 解除双引号,得到apple 
      select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2;
      # 简写,效果同上
      select content->>'$[1]' from test_json where id = 2;
      # 结果:
      +--------------------------------------------+
      | json_unquote(json_extract(content,'$[1]')) |
      +--------------------------------------------+
      | apple                                      |
      +--------------------------------------------+
      +------------------+
      | content->>'$[1]' |
      +------------------+
      | apple            |
      +------------------+
    • - Verschachtelte Daten in JSON abrufen

      Kombiniert mit den beiden vorgestellten Erfassungsmethoden Früher können Sie JSON-Daten erhalten. Verschachtelte Daten in
    • content: id=1

      {"age": 18, "name": "tom", "score": [100, 90, 87], "address": {"city": "Changsha", "province": "Hunan"}}
    • content: id=2
    [1, "apple", „red“, { „age“: 18, „name“: „tom“}]

    # 得到:87
    select content->'$.score[2]' from test_json where id = 1;
    # 结果:
    +-----------------------+
    | content->'$.score[2]' |
    +-----------------------+
    | 87                    |
    +-----------------------+
    
    # 得到:18
    select content->'$[3].age' from test_json where id = 2;
    # 结果:
    +---------------------+
    | content->'$[3].age' |
    +---------------------+
    | 18                  |
    +---------------------+

    4. Besser werden

    - Erhalten Sie Daten von mehreren JSON paths# 🎜🎜#

    kombiniert die Daten mehrerer Pfade in einem Array und gibt

    content: id=1
    {"age": 18, „name“: „tom“, „score“: [100, 90, 87], „address“: {“city“: „Changsha“, „province“: „Hunan“}}


    select json_extract(content,'$.age','$.score') from test_json where id = 1;
    # 结果:
    +-----------------------------------------+
    | json_extract(content,'$.age','$.score') |
    +-----------------------------------------+
    | [18, [100, 90, 87]]                     |
    +-----------------------------------------+
    
    select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1;
    # 结果:
    +----------------------------------------------------------------------+
    | json_extract(content,'$.name','$.address.province','$.address.city') |
    +----------------------------------------------------------------------+
    | ["tom", "湖南", "长沙"]                                              |
    +----------------------------------------------------------------------+
    #🎜 🎜#- Die Verwendung des Pfadausdrucks*

    kombiniert die Daten mehrerer Pfade in einem Array und gibt es zurück

    # 先插入一条用于测试的数据
    INSERT INTO `test_json` (`id`,`content`) VALUES(3,'{"name":"tom","address":{"name":"中央公园","city":"长沙"},"class":{"id":3,"name":"一年三班"},"friend":[{"age":20,"name":"marry"},{"age":21,"name":"Bob"}]}')

    Inhalt: id= 3#🎜🎜 #{"name": "tom", "class": {"id": 3, "name": "Class Three a Year"}, "friend": [{"age": 20, " name": "marry"}, {"age": 21, "name": "Bob"}], "address": {"city": "Changsha", "name": "Central Park"}}#🎜 🎜##🎜 🎜#
    # 获取所有二级嵌套中key=name的值
    # 由于friend的二级嵌套是一个数组,所以.name获取不到其中的所有name值
    select content->'$.*.name' from test_json where id = 3;
    +----------------------------------+
    | content->'$.*.name'              |
    +----------------------------------+
    | ["一年三班", "中央公园"]         |
    +----------------------------------+```
    
    # 获取所有key为name值的数据,包括任何嵌套内的name
    select content->'$**.name' from test_json where id = 3;
    +---------------------------------------------------------+
    | content->'$**.name'                                     |
    +---------------------------------------------------------+
    | ["tom", "一年三班", "marry", "Bob", "中央公园"]         |
    +---------------------------------------------------------+
    
    # 获取数组中所有的name值
    select content->'$.friend[*].name' from test_json where id = 3;
    +-----------------------------+
    | content->'$.friend[*].name' |
    +-----------------------------+
    | ["marry", "Bob"]            |
    +-----------------------------+

    - NULL-Wert zurückgeben

    content: id=1

    {"age": 18, "name": "tom" , "score" : [100, 90, 87], "address": {"city": "Changsha", "province": "Hunan"}}


    Der JSON-Pfad Sie suchen, existiert nicht

    # age路径不存在,返回NULL
    # 若有多个路径,只要有一个路径存在则不会返回NULL
    select json_extract(content,'$.price') from test_json where id = 1;
    +---------------------------------+
    | json_extract(content,'$.price') |
    +---------------------------------+
    | NULL                            |
    +---------------------------------+
    Der Pfad enthält NULL

    # 存在任意路径为NULL则返回NULL
    select json_extract(content,'$.age',NULL) from test_json where id = 1;
    +------------------------------------+
    | json_extract(content,'$.age',NULL) |
    +------------------------------------+
    | NULL                               |
    +------------------------------------+

    - Rückgabefehler

    Wenn der erste Parameter kein JSON-Typ ist Daten, Fehler zurückgeben #🎜🎜 #
    select json_extract('{1,2]',$[0])
    Wenn der Pfadausdruck nicht standardisiert ist, wird ein Fehler zurückgegeben

    select content->'$age' from test_json where id = 1;
    # 结果:
    ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.

    5. Verwendungsszenarien

    Die JSON_EXTRACT-Funktion wird normalerweise verwendet, um einen bestimmten Wert in JSON zu erhalten. Verwenden Sie

    , wenn Sie Daten verwenden oder sie als Beurteilungsbedingung verwenden möchten.

    Das obige ist der detaillierte Inhalt vonSo verwenden Sie json_extract in MySQL. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

    Stellungnahme:
    Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen