Javascript 我想要一种更好的编写SQL的方法,从comments表中获取所有注释,这些注释属于articles表中的特定文章

Javascript 我想要一种更好的编写SQL的方法,从comments表中获取所有注释,这些注释属于articles表中的特定文章,javascript,sql,node.js,postgresql,Javascript,Sql,Node.js,Postgresql,我有一个带有PK_articleid的articles表,还有一个带有引用articles表的FK_articleid的comments表。我希望能够得到属于某篇文章的所有评论。见下表: 到目前为止,我能够通过内部联接获得所需的列,但我希望有一种方法可以访问comments对象属性,如下面的“What i want”示例对象中所示 const text = 'SELECT a.articleid, a.createdon, a.title, a.article, c.id, c.commen

我有一个带有PK_articleid的articles表,还有一个带有引用articles表的FK_articleid的comments表。我希望能够得到属于某篇文章的所有评论。见下表:

到目前为止,我能够通过内部联接获得所需的列,但我希望有一种方法可以访问comments对象属性,如下面的“What i want”示例对象中所示

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
      }
    ]
  }
}

据我所知,您需要获取表属性,而不是存储的数据。
在postgres中,获取表属性(列名称和数据类型)的方法是根据这些文档查询信息模式,这不是最好的方法,而是最接近您的代码的方法(因此,更改较少)


你说你想要“属于某一特定文章”的信息,请在查询结束时替换该信息的文章id(此处为你的文章id)。

Hi-Uwem,这看起来像是nodejs,不是我的专长,但你可能想添加一些关于你正在使用的库和数据库的信息。越具体越容易得到一些答案。查询返回所有文章的注释,而不仅仅是一篇文章。为什么要从
comments
数组的第一行提取文章信息?如果要获取特定文章的评论,需要在查询中使用
WHERE a.articleid=?
。那么,您是说获得的数据比您想要的多吗?我能看到的唯一区别是名称和列数。如果是这种情况,只需将查询更改为仅返回所需内容,并将代码更改为仅读取所需数据(命名为所需数据)。@Barmar使用LEFT JOIN时,查询将返回所有文章的注释,直到我使用INTERNAR JOIN为止。正如您所看到的,它并不健壮,因为我必须提取文章,而不是提取,它会为每个评论返回相同的文章。也许在这里加入并不理想。这对我很有效。虽然有点粗糙,但如果我在接下来的一小时内不能想出更好的查询语句,我会接受它。谢谢。很酷,很高兴知道它有用。更好的方法是有两个查询:一个用于文章,一个用于评论,这样您就不必在响应上拆分结果。这需要更多的工作来处理响应状态,但这将更加清晰。我认为他想要的输出中的数据类型只是占位符,他实际上想要数据。
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
WHERE a.articleid = YOUR_ARTICLE_ID_HERE';

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.map((row) => {return {
          commentId: row.commentId,
          comment: row.comment,
          authorId: row.authorId,
        }})
      },
    });
  });
};