使用mysql从json对象中选择数据

htrmnn0y  于 2023-01-27  发布在  Mysql
关注(0)|答案(1)|浏览(170)

我有一个包含两列的表:
1.客户ID

  1. json_data(包含JSON对象)如下所示
{
  "nameValuePairs": {
    "CONTACTS": {
      "nameValuePairs": {
        "contacts": {
          "values": [
            {
              "nameValuePairs": {
                "contact_id": "1",
                "contact_phoneNumber": "080000016",
                "contact_phoneNumberCategory": "Mobile",
                "contact_firstName": "Huawei Customer Service",
                "contact_last_name": "Huawei Customer Service",
                "contact_title": "Huawei Customer Service",
                "contact_email": "mobile.pk@huawei.com"
              }
            },
            {
              "nameValuePairs": {
                "contact_id": "2",
                "contact_phoneNumber": "15",
                "contact_phoneNumberCategory": "Mobile",
                "contact_firstName": "Police Helpline",
                "contact_last_name": "Police Helpline",
                "contact_title": "Police Helpline"
              }
            },
            {
              "nameValuePairs": {
                "contact_id": "3",
                "contact_phoneNumber": "16",
                "contact_phoneNumberCategory": "Mobile",
                "contact_firstName": "Fire Brigade Helpline",
                "contact_last_name": "Fire Brigade Helpline",
                "contact_title": "Fire Brigade Helpline"
              }
            }
          ]
        }
      }
    }
  }
}

现在,我想使用MySQL提取信息,以便获得如下表
| 客户ID|联系人_职务|
| - ------|- ------|
| 1个|华为帮助热线|
| 1个|警察|
我怎样才能得到上面的表格?我需要查询
我尝试以下查询

JSON_EXTRACT(json_data, '$.nameValuePairs.CONTACTS.nameValuePairs.contacts.values[0]
                          .nameValuePairs.contact_title') AS "Contact name",

我要这张table
| 客户标识|联系人_职务|
| - ------|- ------|
| 1个|【"华为求助热线"、"警察"、"消防队"】|
| 第二章|【"华为求助热线"、"警察"、"消防队"】|
我不需要此表。我需要每行一个contact_title,与同一个customer_id对应

ruarlubt

ruarlubt1#

如果数据库版本是8.0+,则可以将JSON_TABLE()函数与 * 交叉连接 * 沿着使用,例如

SELECT contact_id, contact_title
  FROM `tab`
  JOIN JSON_TABLE(
                 `json_data`,
                 '$.nameValuePairs.CONTACTS.nameValuePairs.contacts.values[*]
                   .nameValuePairs' 
                  COLUMNS (
                           contact_id VARCHAR(100) PATH '$.contact_id'
                          )
       ) j1
  JOIN JSON_TABLE(
                  `json_data`,
                  '$.nameValuePairs.CONTACTS.nameValuePairs.contacts.values[*]
                    .nameValuePairs' 
                   COLUMNS (
                            contact_title VARCHAR(100) PATH '$.contact_title'
                           )
             ) j2

或者更干净的选择可以是

WITH t AS
(  
 SELECT contact_id, contact_title
   FROM `tab`
   JOIN JSON_TABLE(
                  `json_data`,
                  '$.nameValuePairs.CONTACTS.nameValuePairs.contacts.values[*]
                    .nameValuePairs' 
                   COLUMNS (
                            contact_id VARCHAR(100) PATH '$.contact_id',
                            contact_title VARCHAR(100) PATH '$.contact_title'
                           )
        ) AS j
)
SELECT t2.contact_id, t1.contact_title
  FROM t AS t1
  JOIN t AS t2

Demo

相关问题