node.js - Postgres 将 count(*) 转换为整数?

标签 node.js postgresql sequelize.js postman

我有一个返回计数的简单查询。

select count(*)
from table
wehre ....
我希望计数返回为整数而不是字符串。 Postgres 中的默认值似乎是 String。
我试图这样做:
select cast(count(*) as INTEGER)
....
但它不起作用。查询运行,但计数的类型仍然是一个字符串。我试过其他方法都没有成功。
有没有办法让计数返回为数字而不是字符串类型?
编辑:
当我在 DataGrip 中检查结果表的类型时,它确实显示计数返回的类型为“count: Int ”。
让我失望的是 Postman 返回的结果。
查询选择是这样的:
select person.age, count(*) from ....
当该查询在它所在的端点中运行时,Postman 将显示以下结果:[{"age":18, count: "45"}, ....]所以,然后我假设 Postgres 的计数形式作为字符串返回,因为 age(表上的整数类型)确实作为数字类型而不是字符串返回。所以,现在我很困惑为什么一种列类型是正确的,而另一种则不是(计数......)。
使用 Sequelize 运行查询:
const result = await sequelize.query(query, { type: sequelize.QueryTypes.SELECT });
然后直接通过以下方式返回:
res.status(200).json(result)

最佳答案

As the documentation states :

count ( * ) → bigint

Computes the number of input rows. Yes

count 返回类型 bigint 。不是字符串类型。

关于node.js - Postgres 将 count(*) 转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66389300/

相关文章:

javascript - 从 HTML 应用程序通过 TCP IP 客户端发送短字符串的最佳方式

node.js - 如何在没有任何 json 文件的情况下使用 Firebase Auth 进行身份验证?

node.js - 无法使用 WebSocket 连接到 node.js 检查器

postgresql - PostgreSQL 中的物化 View 可以继承吗?

sql - 如何优化 Postgres 中存储过程的性能?

node.js - :M eager loading (include) using sequelizejs

javascript - 为什么 Chrome 在 "strict mode"中使用 block 内的函数时仍然保持沉默?

postgresql - 我想列出所有日期及其各自的数据

node.js - 为 postgresQL 续写 bulkCreate updateOnDuplicate?