简体   繁体   English

如何编写查询以基于时间戳从数据库获取数据

[英]How to write a query to fetch data from DB on the basis of timestamp

I have a task to do in which i have to write a query which can fetch the data on the basis of timestamp 我有一个任务要做,其中我必须编写一个查询,该查询可以根据时间戳获取数据

  1. i have a table in my db having two columns one is event and other one is Time 我的数据库中有一个表,其中有两列,一个是event ,另一个是Time
  2. what i am trying to do is to write a query which can give me data entered in last 1 minutes 我想做的是编写一个查询,该查询可以为我提供最近1分钟内输入的数据
  3. I have written a query which is giving me correct result but in sql 5.1 我写了一个查询,给我正确的结果,但在SQL 5.1
  4. i am using Derby data base and facing issue to get the output 我正在使用Derby数据库并面临问题以获取输出

这是我的数据库

this is the error i am getting from running my query in derby 这是我在derby中运行查询时遇到的错误

error Syntax error: Encountered "5" error Syntax error: Encountered "5"

my query is SELECT * FROM test WHERE Time >= CURRENT_TIMESTAMP - INTERVAL 5 minute AND Time <= CURRENT_TIMESTAMP 我的查询是SELECT * FROM test WHERE Time >= CURRENT_TIMESTAMP - INTERVAL 5 minute AND Time <= CURRENT_TIMESTAMP

I'm not sure about the interval syntax, but you can try TIMESTAMPADD() : 我不确定间隔语法,但是您可以尝试TIMESTAMPADD()

SELECT *
FROM test
WHERE Time >= {fn TIMESTAMPADD(SQL_TSI_MINUTE, -5, CURRENT_TIMESTAMP) }
AND Time <= CURRENT_TIMESTAMP

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

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