简体   繁体   English

如何编写nodeJS查询以使用datetime INTERVAL获取postgreSQL数据库的最近24小时记录

[英]How to write nodeJS query to fetch last 24 hours records of postgreSQL database using datetime INTERVAL

I am fetching last 24 hours records from PostgreSQL DB using CURRENT_TIMESTAMP - INTERVAL '24 hours' in my .js file. 我在我的.js文件中使用CURRENT_TIMESTAMP - INTERVAL '24 hours'从PostgreSQL DB获取最近24小时的记录。 But I am not able to write it properly due to single inverted commas in query. 但由于查询中有单个引号,我无法正确编写。

I am fetching records from PostgreSQL database and showing them in terms of bar chart using angular 6. 我从PostgreSQL数据库中获取记录,并使用angular 6以条形图显示它们。

Here is the query I am trying to run from .js file to fetch last 24 hours records: 这是我试图从.js文件运行以获取最近24小时记录的查询:

query = {

    name: 'fetch-chart-data24',

    text: ' SELECT availablecar, recentTime, adrs FROM stations_logs WHERE adrs = $1 AND recentTiontime > CURRENT_TIMESTAMP - INTERVAL '24 hours' ORDER BY recentTime desc',

    values: [req.body.adrs,req.body.param]
}

Final result from query should be most recent 24 hours records for database. 查询的最终结果应该是最近24小时的数据库记录。 Currently I am getting error as 目前我收到错误

Unexpected number 意外的号码

You can escape single quotes with back slahes ( \\ ) like this: 您可以使用反斜杠( \\ )来escape单引号,如下所示:

{
  ...
  text: ' SELECT availablecar, recentTime, adrs FROM stations_logs WHERE adrs = $1 AND recentTiontime > CURRENT_TIMESTAMP - INTERVAL \'24 hours\' ORDER BY recentTime desc',
  ...
}

For further reading: 进一步阅读:

https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-javascript#conclusion https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-javascript#conclusion

query = {
    name: 'fetch-chart-data24',
    text: `SELECT availablecar, recentTime, adrs FROM stations_logs WHERE adrs = $1 AND recentTiontime > CURRENT_TIMESTAMP - INTERVAL '24 hours' ORDER BY recentTime desc`,
    values: [req.body.adrs,req.body.param]
}

You can make use of template literals . 您可以使用模板文字 Its for embedding expressions, but can satisfy your use case too. 它用于嵌入表达式,但也可以满足您的用例。

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

相关问题 如何使用 nodejs 从 mysql 数据库中获取最新的 10 条记录? - How to fetch latest 10 records from mysql database using nodejs? Cassandra +在查询中使用获取最新记录 - Cassandra + Fetch the last records using in query 如何使用mongoose获取最近24小时内创建的mongo文档? - How to get mongo documents created in the last 24 hours using mongoose? 如何在Node.js上为Postgresql数据库编写单元测试 - How to write unit test for postgresql database on nodejs 如何使用NodeJ从Postgresql获取数据? - How to fetch data from postgresql using NodeJs? 使用nodejs查询多个记录并插入数据库 - query multiple records and insert into database using nodejs mongoDB 使用 $match 和 $group 查询(过滤过去 24 小时记录的数据和具有 id 和计数的组) - mongoDB query using $match and $group (filter last 24 hours logged data and group with id and count) Mongoose 查询过去 24 小时内获得点赞的文档 - Mongoose query for documents that got likes on last 24 hours 在Node.js中使用postgresql时,应如何从Web服务器查询数据库? - When using postgresql in Nodejs, how should I query the database from the webserver? 如何检索过去 24 小时的帖子,然后按点击次数和时间戳对它们进行排序? - How to retrieve posts of the last 24 hours and then sort them by clicks and then by timestamps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM