我有一个查询,该查询获取有关某个对象的一些元数据,然后获取对该对象进行的注释。我希望我的express框架将注释作为一个嵌套对象发送回去,但是它将发送多个具有重复数据的“行”。
查询结果/响应发送给客户端:
[ RowDataPacket {
[0] first_name: 'bob',
[0] last_name: 'masd',
[0] corkboardID: 1,
[0] title: 'asd',
[0] datetime_created: 2018-11-21T17:37:02.000Z,
[0] image_url: 'http://example.com',
[0] description: 'asdf',
[0] tags: 'awesome,cool',
[0] WriterFname: 'billy',
[0] WriterLname: 'flimbob',
[0] text: 'M COMMENT',
[0] CmtWrittenAt: 2018-11-21T17:37:02.000Z },
[0] RowDataPacket {
[0] first_name: 'bob',
[0] last_name: 'masd',
[0] corkboardID: 1,
[0] title: 'asd',
[0] datetime_created: 2018-11-21T17:37:02.000Z,
[0] image_url: 'http://example.com',
[0] description: 'asdf',
[0] tags: 'awesome,cool',
[0] WriterFname: 'jim',
[0] WriterLname: 'lurk',
[0] text: 'RESPONSE',
[0] CmtWrittenAt: 2018-11-21T17:37:03.000Z } ]
这是我查询中的相关内容
SELECT
...
GROUP_CONCAT(DISTINCT Tag.name) AS tags,
Cmt.first_name AS WriterFname,
Cmt.last_name AS WriterLname,
Cmt.text,
Cmt.datetime_created AS CmtWrittenAt
FROM
...
INNER JOIN (
SELECT
User.first_name,
User.last_name,
Comment.text,
Comment.datetime_created
FROM
Comment
INNER JOIN User ON User.userID = Comment.userID
WHERE
Comment.pushpinID = ?
) AS Cmt
...
GROUP BY
...
WriterFname,WriterLname,CmtWrittenAt,Cmt.text
如您所见,我已经将“标签”分组并作为数组发送回去,并且我希望将Comment / Cmt分组为嵌套对象。
我已经通过使用JSON_OBJECT('fname',Cmt.first_name,'lname',Cmt.last_name,'text',Cmt.text,'time',Cmt.datetime_created) AS Comment
接近了,它给出了以下内容:
[0] [ RowDataPacket {
[0] first_name: 'zxc',
[0] last_name: 'zxc',
[0] corkboardID: 1,
[0] title: 'asd',
[0] datetime_created: 2018-11-21T17:37:02.000Z,
[0] image_url: 'http://example.com',
[0] description: 'asdf',
[0] tags: 'awesome,cool',
[0] Comment:
[0] '{"text": "M COMMENT", "time": "2018-11-21 09:37:02.000000", "fname": "asd", "lname": "asd"}' },
[0] RowDataPacket {
[0] first_name: 'zxc',
[0] last_name: 'zxc',
[0] corkboardID: 1,
[0] title: 'asd',
[0] datetime_created: 2018-11-21T17:37:02.000Z,
[0] image_url: 'http://example.com',
[0] description: 'asdf',
[0] tags: 'awesome,cool',
[0] Comment:
[0] '{"text": "RESPONSE", "time": "2018-11-21 09:37:03.000000", "fname": "qwe", "lname": "qwe"}' } ]
但这仍然有很多重复的数据