我想要两个 mysql 表加入 group_concat 结果在 json 数组

Posted

技术标签:

【中文标题】我想要两个 mysql 表加入 group_concat 结果在 json 数组【英文标题】:i want two mysql table join with group_concat result in json array 【发布时间】:2020-02-15 02:16:50 【问题描述】:

我想要两个带有 group_concat 的 mysql 表连接,结果是嵌套的 json 数组格式,我的表名 post[post_id, user_id, description, created_date] 和 files[post_id, saved_name] 一切都很好,但只有 file_name 值不是数组格式

输出

"file_name":"3.jpg, 2.jpg"

所需输出

"file_name":["1.jpg", "2.jpg", "3.jpg"] json 数组中的值 我正在尝试使用 JSON_OBJECT 的 mysql,但出现错误

SELECT JSON_OBJECT('post_id', T1.post_id, 'user_id', T1.user_id,
                   JSON_ARRAYAGG(
        JSON_OBJECT('post_id', T2.post_id, 'saved_name', T2.saved_name,
GROUP_CONCAT(T2.saved_name ORDER BY  T2.post_id ASC)))) AS 'file_Name' 
FROM posts AS T1 INNER JOIN files AS T2 ON T1.post_id = T2.post_id   
group by T1.post_id ORDER BY created_date DESC

1582 - 调用本机函数“JSON_OBJECT”时的参数计数不正确

  #Current Out PUT  
    
        "status": 200,
        "error": null,
        "res_posts": [
            "post_id": 3,
            "user_id": 1,
            "description": " Working a Fine ",
            "post_type": 0,
            "created_date": "2019-01-25T18:40:41.000Z",
            "saved_name": "8.jpg",
            "file_Name": "7.jpg,8.jpg"
        , 
            "post_id": 2,
            "user_id": 1,
            "description": " Hello hi",
            "post_type": 1,
            "created_date": "2019-01-21T12:51:16.000Z",
            "saved_name": "4.jpg",
            "file_Name": "6.jpg,5.jpg,4.jpg"
        , 
            "post_id": 1,
            "user_id": 1,
            "description": " Hi How are you ",
            "post_type": 0,
            "created_date": "2019-01-21T12:50:51.000Z",
            "saved_name": "1.jpg",
            "file_Name": "3.jpg,2.jpg,1.jpg"
        ]
    





 #Required OUT PUT:



    "status": 200,
    "error": null,
    "res_posts": [
        "post_id": 3,
        "user_id": 1,
        "description": " Working a Fine ",
        "post_type": 0,
        "created_date": "2019-01-25T18:40:41.000Z",
        "saved_name": "8.jpg",
        "file_Name": ["7.jpg", "8.jpg"]
    , 
        "post_id": 2,
        "user_id": 1,
        "description": " Hello hi",
        "post_type": 1,
        "created_date": "2019-01-21T12:51:16.000Z",
        "saved_name": "4.jpg",
        "file_Name": ["6.jpg","5.jpg","4.jpg"]
    , 
        "post_id": 1,
        "user_id": 1,
        "description": " Hi How are you ",
        "post_type": 0,
        "created_date": "2019-01-21T12:50:51.000Z",
        "saved_name": "1.jpg",
        "file_Name": ["3.jpg","2.jpg","1.jpg"]
    ]

我当前的查询:

SELECT  T1.*, T2.post_id, T2.saved_name, 
GROUP_CONCAT(T2.saved_name ORDER BY T2.post_id ASC) AS 'file_Name' 
FROM posts AS T1 INNER JOIN files AS T2 ON T1.post_id = T2.post_id   
group by T1.post_id ORDER BY created_date DESC

必填查询

SELECT JSON_OBJECT('post_id', T1.post_id, 'user_id', T1.user_id,
                   JSON_ARRAYAGG(
        JSON_OBJECT('post_id', T2.post_id, 'saved_name', T2.saved_name,
GROUP_CONCAT(T2.saved_name ORDER BY  T2.post_id ASC)))) AS 'file_Name' 
FROM posts AS T1 INNER JOIN files AS T2 ON T1.post_id = T2.post_id   
group by T1.post_id ORDER BY created_date DESC

1582 - 调用本机函数“JSON_OBJECT”时的参数计数不正确

【问题讨论】:

【参考方案1】:

变化:

JSON_OBJECT(
  'post_id', T2.post_id,
  'saved_name', T2.saved_name,
  GROUP_CONCAT(T2.saved_name ORDER BY  T2.post_id ASC)
) AS 'file_Name'

通过

JSON_OBJECT(
  'post_id', T2.post_id,
  'saved_name', MAX(T2.saved_name),
  'file_Name', GROUP_CONCAT(T2.saved_name ORDER BY  T2.post_id ASC)
)

见dbfiddle。

更新

类似:

["7.jpg", "6.jpg", "8.jpg"]

不被视为有效的 JSON。

试试:

JSON_OBJECT(
  'post_id', `post_id`,
  'saved_name', MAX(`saved_name`),
  'file_Name', JSON_ARRAYAGG(CONCAT('', `saved_name`, ''))
) `JSON`

见dbfiddle。

【讨论】:

SELECT JSON_OBJECT('post_id', T1.post_id, 'user_id', T1.user_id, JSON_ARRAYAGG( JSON_OBJECT( 'post_id', T2.post_id, 'saved_name', MAX(T2.saved_name), 'file_Name', GROUP_CONCAT(T2.saved_name ORDER BY T2.post_id ASC))) ) FROM posts AS T1 INNER JOIN files AS T2 ON T1.post_id = T2.post_id group by T1.post_id ORDER BY created_date DESC 在使用您的建议后得到同样的错误,并且有两个表格帖子和文件 Getting OutPut "file_name":"3.jpg, 2.jpg" 要求 OutPut "file_name":["3.jpg", "2.jpg"] 先生,谢谢您的回复,我正在使用两个表格帖子和文件,我正在加入这两个表格。我需要文件名的 json 数组

以上是关于我想要两个 mysql 表加入 group_concat 结果在 json 数组的主要内容,如果未能解决你的问题,请参考以下文章

MySQL:以特定顺序连接两个表的不同行?

我如何查询给定日期范围的mysql并加入两个表?

MySQL:加入表并根据另一列上的聚合函数从一行返回一列[重复]

mysql查询加入,比较两个表并返回第一个表中的所有记录

使用 MySQL JSON 字段加入表

Laravel 加入 Auth 表