如何使用NodeJS在Postgres中合并多行

时间:2018-11-28 18:18:40

标签: node.js postgresql

使用nodeJS如何在json输出中将多行连接为一个字符串值?现在它输出多行,但我想将值合并为一行。

      GTIN     |       Name       |       TypeCode      |     Value
--------+----------------+------------------+------------------------
26001087006846 |   Product Name   |        Name1        |       1
26001087006846 |   Product Name   |        Name2        |       2
26001087006846 |   Product Name   |        Name3        |       3
26001087006846 |   Product Name   |        Name4        |       4

期望的结果

[{"GTIN":"26001087006846","Name":"Product Name","Name1":"1","Name2":"2","Name3":"3","Name4":"4"}]

查询语句

 pool.query(
  'SELECT table_name."GTIN" FROM table_name WHERE table_name."GTIN" = $1'
    , [id], (error, results) => {
    if (error) {
      throw error
    }
    response.status(200).json(results.rows)
  })

0 个答案:

没有答案