gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:22:36 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com