如何用group-concat mysql创建json格式?

ndasle7k  于 2022-12-26  发布在  Mysql
关注(0)|答案(8)|浏览(254)

如何用group-concat mysql创建json格式?
(我使用MySQL)
例一:
表一:

email            |    name  |   phone
-------------------------------------
my1@gmail.com    | Ben      | 6555333
my2@gmail.com    | Tom      | 2322452
my2@gmail.com    | Dan      | 8768768
my1@gmail.com    | Joi      | 3434356

比如语法代码没有给出格式:

select email, group-concat(name,phone) as list from table1 
group by email

我需要的输出:

email         |    list
------------------------------------------------
my1@gmail.com |  {name:"Ben",phone:"6555333"},{name:"Joi",phone:"3434356"}
my2@gmail.com |  {name:"Tom",phone:"2322452"},{name:"Dan",phone:"8768768"}

谢啦,谢啦

brvekthn

brvekthn1#

对于较新版本的MySQL,您可以使用JSON_OBJECT函数来实现所需的结果,如下所示:

GROUP_CONCAT(
  JSON_OBJECT(
    'name', name,
    'phone', phone
  )
) AS list

要使SQL响应准备好作为数组进行分析:

CONCAT(
  '[',
  GROUP_CONCAT(
    JSON_OBJECT(
      'name', name,
      'phone', phone
    )
  ),
  ']'
) AS list

这将为您提供如下字符串:[{name: 'ABC', phone: '111'}, {name: 'DEF', phone: '222'}],可以进行JSON解析。

disbfnqx

disbfnqx2#

试试这个查询,

SELECT
  email,
  GROUP_CONCAT(CONCAT('{name:"', name, '", phone:"',phone,'"}')) list
FROM
  table1
GROUP BY
  email;

JSON格式结果-

+---------------+-------------------------------------------------------------+
| email         | list                                                        |
+---------------+-------------------------------------------------------------+
| my1@gmail.com | {name:"Ben", phone:"6555333"},{name:"Joi", phone:"3434356"} |
| my2@gmail.com | {name:"Tom", phone:"2322452"},{name:"Dan", phone:"8768768"} |
+---------------+-------------------------------------------------------------+
falq053o

falq053o3#

适用于Mysql 5.7.22以上版本

SELECT
        email,
        JSON_ARRAYAGG(
            JSON_OBJECT(
                'name', name,
                'phone', phone
            )
        ) AS list
    FROM table1
    GROUP BY email;

结果:

+---------------+-------------------------------------------------------------------+
| email         | list                                                              |
+---------------+-------------------------------------------------------------------+
| my1@gmail.com | [{"name":"Ben", "phone":6555333},{"name":"Joi", "phone":3434356}] |
| my2@gmail.com | [{"name":"Tom", "phone":2322452},{"name":"Dan", "phone":8768768}] |
+---------------+-------------------------------------------------------------------+

唯一的区别是列list现在是Json-valid,因此可以直接解析为Json

nkoocmlb

nkoocmlb4#

希望这能找到合适的眼睛。
您可以使用:

    • 对于阵列**(文档):
JSON_ARRAYAGG(col_or_expr) as ...
    • 对于对象**(文档):
JSON_OBJECTAGG(key, value) as ...
5w9g7ksd

5w9g7ksd5#

Devart上面的回答很好,但是K2xL的问题是有效的。我找到的答案是使用HEX()对name列进行十六进制编码,这确保了它将创建有效的JSON。然后在应用程序中,将十六进制转换回字符串。
(抱歉的自我推销,但是)我写了一个小博客文章关于这一点更详细:http://www.alexkorn.com/blog/2015/05/hand-rolling-valid-json-in-mysql-using-group_concat/
[Edit对于Oriol]下面是一个示例:

SELECT email,
    CONCAT(
        '[',
        COALESCE(
            GROUP_CONCAT(
                CONCAT(
                    '{',
                    '\"name\": \"', HEX(name), '\", ',
                    '\"phone\": \"', HEX(phone), '\"',
                    '}')
                ORDER BY name ASC
                SEPARATOR ','),
            ''),
        ']') AS bData
FROM table
GROUP BY email

还请注意,我已经添加了一个COALESCE的情况下,没有该电子邮件的项目。

xoshrz7s

xoshrz7s6#

类似于Madacol上面的回答,但稍有不同。除了JSONARRAYagg,你还可以使用CAST AS JSON:

SELECT
        email,
       CAST( CONCAT(
        '[', 
           GROUP_CONCAT(
           JSON_OBJECT(
              'name', name,
              'phone', phone
            )
        ),']') AS JSON )
    FROM table1
    GROUP BY email;
    • 结果:**
+---------------+-------------------------------------------------------------------+
| email         | list                                                              |
+---------------+-------------------------------------------------------------------+
| my1@gmail.com | [{"name":"Ben", "phone":6555333},{"name":"Joi", "phone":3434356}] |
| my2@gmail.com | [{"name":"Tom", "phone":2322452},{"name":"Dan", "phone":8768768}] |
+---------------+-------------------------------------------------------------------+
t30tvxxf

t30tvxxf7#

偏离@Devart的答案......如果字段包含换行符或双引号,结果将不是有效的JSON。
因此,如果我们知道“phone”字段偶尔会包含双引号和换行符,那么我们的SQL将如下所示:

SELECT
  email,
  CONCAT(
    '[',
    GROUP_CONCAT(CONCAT(
        '{name:"', 
        name, 
        '", phone:"', 
        REPLACE(REPLACE(phone, '"', '\\\\"'),'\n','\\\\n'), 
        '"}'
      )),
    ']'
  ) AS list
FROM table1 GROUP BY email;

如果Ben phone的中间有一个引号,而Joi的中间有一个换行符,那么SQL将给予如下(有效JSON)结果:

[{name:"Ben", phone:"655\"5333"},{name:"Joi", phone:"343\n4356"}]
6vl6ewon

6vl6ewon8#

像这样使用

SELECT email,concat('{name:"',ur_name_column,'",phone:"',ur_phone_column,'"}') as list FROM table1 GROUP BY email;

干杯

相关问题