昨天在postgresql中的记录

时间:2013-07-28 19:39:51

标签: postgresql

我有一个应用程序,我使用过MySQL。我的报告延长了过去24小时的记录。我使用了查询:

WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))

现在我必须使用PostgreSQL并且不知道如何报告过去24小时。 你们有人可以帮忙吗?

3 个答案:

答案 0 :(得分:59)

WHERE workorder.createdtime > current_date - 1     -- Yesterday and today

WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr

答案 1 :(得分:28)

> TIMESTAMP 'yesterday'

为方便起见,Postgres包含一些硬编码值special Date/Time inputs。它们包括:

  • yesterday
  • today
  • tomorrow
  • now

尝试SELECT TIMESTAMP 'now'

例如,这是一个查询。

SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday' 
ORDER BY when_row_created_ DESC
;

这些命令可能不适合生产代码,但它们在开发中肯定很方便。阅读文档并进行一些练习,以确保您了解这些命令的行为,会话的时区如何影响它们等等。

缺点包括(a)隐含地忽略时区的关键问题,(b)不是标准SQL。

答案 2 :(得分:6)

where workorder.createdtime >= now() - interval '24 hour'