Postgres sequelize 原始查询以获取计数返回字符串值

我在我的 node-express 服务器中使用 postgresql sequelize.

I am using postgresql sequelize in my node-express server.

当我运行以下 SQL 命令时,我得到一个字符串值作为结果.

When I run the following SQL Command, I am getting a string value for result.

我想获取此计数的 整数 值.

I want to get Integer value for this count.

SELECT count(id) from collaborators where id<3

结果:

count =  [ { count: '1' } ]

有什么方法可以获取结果的数值吗?还是我总是需要使用 parseInt(count, 10) 来获取 int 值?

Is there any way to get number values for result? Or do I always need to use parseInt(count, 10) to get int value?

感谢您的帮助!

推荐答案

@DemtriOS 正确的是 count 返回 bigint 因此解释为 string,您可以直接在查询中直接对它进行类型转换,如下所示:

@DemtriOS is correct that count returns bigint hence interpreted into string, you can directly typecast it directly on your query like so:

SELECT COUNT(id)::int
FROM collaborators
WHERE id < 3

相关文章