[英]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.