如何编写SQL以获取过去30天中来自指定表的所有数据,而不必手动指定日期?我有一张桌子,如下所示:
school
(
school_id bigint,school_name character varying(100),
school_code character varying(10),
created_time timestamp with time zone,
)
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取created_time
过去30天内的所有记录。我正在尝试类似以下的操作,但是它对我不起作用:
select * from school where created_time > '2017-09-31 11:12:57.425+00';
Run Code Online (Sandbox Code Playgroud)
另外,我不想手动指定日期,我想通过自动获取当前日期来获取最近30天的数据。有什么办法可以做到这一点?
我得到的当前错误如下:
ERROR: date/time field value out of range: "2017-09-31 11:12:57.425+00"
LINE 1: select * from school where created_time > '2017-09-31...
^
********** Error **********
ERROR: date/time field value out of range: "2017-09-31 11:12:57.425+00"
SQL state: 22008
Character: 50
Run Code Online (Sandbox Code Playgroud)
只需使用current_timestamp
(在此处记录):
select *
from school
where created_time > current_timestamp - interval '30 day';
Run Code Online (Sandbox Code Playgroud)