简体   繁体   English

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

[英]i want two mysql table join with group_concat result in json array

i want two mysql table join with group_concat result in nested json array format, My table names posts[post_id, user_id, description, created_date] and files[post_id, saved_name] every thing is good but only file_name values are not array format我想要两个 mysql 表连接与 group_concat 导致嵌套的 json 数组格式,我的表名 post[post_id, user_id, description, created_date] 和 files[post_id, saved_name] 一切都很好,但只有 file_name 值不是数组格式

OutPut OutPut

"file_name":"3.jpg, 2.jpg" “文件名”:“3.jpg,2.jpg”

Required OutPut需要 OutPut

"file_name":["1.jpg", "2.jpg", "3.jpg"] values in json array i am trying mysql with JSON_OBJECT, but getting errors "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 - Incorrect parameter count in the call to native function 'JSON_OBJECT' 1582 - 对本机 function '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"]
    }]
}

my current query:我当前的查询:

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

Required query必填查询

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 - Incorrect parameter count in the call to native function 'JSON_OBJECT' 1582 - 对本机 function 'JSON_OBJECT' 的调用中的参数计数不正确

Change:改变:

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'

by经过

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)
)

See dbfiddle .请参阅dbfiddle

UPDATE更新

Something like:就像是:

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

isn't considered a valid JSON.不被视为有效的 JSON。

Try:尝试:

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

See dbfiddle .请参阅dbfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM