【发布时间】:2019-11-14 14:15:03
【问题描述】:
我有一个带有 PK_articleid 的文章表,以及一个带有 FK_articleid 的 cmets 表,它引用了文章表。我希望能够获得属于特定文章的所有 cmets。见下表:
到目前为止,我能够通过 INNER JOIN 获得所需的列,但我想要一种访问 cmets 对象属性的方法,如下面的“我想要的”示例对象中所示。
const text = 'SELECT a.articleid, a.createdon, a.title, a.article, c.id, c.comment, c.authorid
FROM articles a INNER JOIN comments c ON a.articleid = c.articleid';
exports.getArticleAndComments = (request, response) => {
pool.query(text, (error, res) => {
if (error) {
// throw error
console.log(`not able to get connection ${error}`);
response.status(400).json({
status: 'error',
error: error.stack,
});
}
response.status(200).json({
status: 'success',
data: {
id: res.rows[0].articleid,
createdon: res.rows[0].createdon,
title: res.rows[0].title,
article: res.rows[0].article,
comments: res.rows
},
});
});
};
我想要什么:
"data" : {
"id" : Integer ,
"createdOn" : DateTime ,
"title" : String ,
"article" : String ,
"comments" : [
{
"commentId" : Integer ,
"comment" : String ,
"authorId" : Integer ,
} ,
{
"commentId" : Integer ,
"comment" : String ,
"authorId" : Integer ,
} ,
]
}
我得到了什么:
{
"status": "success",
"data": {
"id": 21,
"createdon": "2019-11-13T19:41:51.613Z",
"title": "The gods must be crazy",
"article": "An article about crazy gods",
"comments": [
{
"articleid": 21,
"createdon": "2019-11-13T19:41:51.613Z",
"title": "The gods must be crazy",
"article": "An article about crazy gods",
"commentId": 28,
"comment": "The gods have always been crazy",
"authorid": 106
},
{
"articleid": 21,
"createdon": "2019-11-13T19:41:51.613Z",
"title": "The gods must be crazy",
"article": "An article about crazy gods",
"commentId": 27,
"comment": "Unleash the dragon",
"authorid": 106
}
]
}
}
【问题讨论】:
-
嗨,Uwem,这看起来像 nodejs,这不是我的强项,但您可能想添加一些有关您正在使用的库和数据库的信息。越具体越容易得到一些答案。
-
查询返回所有文章的 cmets,而不仅仅是单个文章。为什么要从
comments数组的第一行提取文章信息? -
如果要获取特定文章的cmets,查询中需要
WHERE a.articleid = ?。 -
那么你是说你得到的数据比你想要的多吗?我能看到的唯一区别是名称和列数。如果是这种情况,那么只需将您的查询更改为仅返回您想要的内容,并将您的代码更改为仅读取您想要的数据,并根据需要命名。
-
@Barmar 与 LEFT JOIN,查询返回所有文章的 cmets,直到我使用 INNER JOIN。如您所见,它并不可靠,因为我必须提取文章,而不提取,它会为每条评论返回相同的文章。也许 JOIN 在这里并不理想。
标签: javascript sql node.js postgresql