postgresql postrgesql 中昨天的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17912088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:19:04  来源:igfitidea点击:

records from yesterday in postrgesql

postgresql

提问by Krokodyle

I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:

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

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

Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours. Can any of you help?

现在我必须使用PostgreSQL,不知道如何制作过去24小时的报告。你们中的任何人都可以帮忙吗?

回答by Curt

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

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

回答by Basil Bourque

> TIMESTAMP 'yesterday'

> TIMESTAMP 'yesterday'

For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:

为方便起见,Postgres 包含一些硬编码值作为特殊的日期/时间输入。它们包括:

  • yesterday
  • today
  • tomorrow
  • now
  • yesterday
  • today
  • tomorrow
  • now

Try SELECT TIMESTAMP 'now'.

试试SELECT TIMESTAMP 'now'

For example, here is a query.

例如,这里是一个查询。

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

These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session's time zone affects them and so on.

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

Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.

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

回答by Roman Pekar

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