简体   繁体   English

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

[英]Postgres sequelize raw query to get count returns string value

I am using postgresql sequelize in my node-express server.我在 node-express 服务器中使用 postgresql sequelize。

When I run the following SQL Command, I am getting a string value for result.当我运行以下 SQL 命令时,我得到一个字符串值作为结果。

I want to get Integer value for this count.我想获得这个计数的数值。

SELECT count(id) from collaborators where id<3

Result:结果:

count =  [ { count: '1' } ]

Is there any way to get number values for result?有没有办法获得结果的数值? Or do I always need to use parseInt(count, 10) to get int value?还是我总是需要使用parseInt(count, 10)来获取 int 值?

I appreciate any help!我感谢任何帮助!

According to this https://www.postgresql.org/docs/8.2/static/functions-aggregate.html , the count() function returns bigint type.根据这个https://www.postgresql.org/docs/8.2/static/functions-aggregate.html , count() 函数返回 bigint 类型。 Because of this it will get interpreted as a string.因此,它将被解释为字符串。 It looks like explicit conversion to an int is what you will have to do.看起来显式转换为 int 是您必须做的。 So, parseInt(count, 10) it is.所以, parseInt(count, 10)是。

@DemtriOS is correct that count returns bigint hence interpreted into string , you can directly typecast it directly on your query like so: @DemtriOS是正确的, count返回bigint因此解释为string ,您可以直接在查询中直接键入它,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM