postgresql 按日期范围查询

目录

 

一、按日期范围查询

二、查询近几天的数据


一、按日期范围查询

方法一:

select * from user_info where create_date >= '2015-07-01' and create_date < '2015-08-15';

方法二:为啥字符串可以按日期格式比较大小

select * from user_info where create_date between '2015-07-01' and '2015-08-15';

方法三:

select * from user_info where create_date >= '2015-07-01'::timestamp and create_date < '2015-08-15'::timestamp;

方法四:

select * from user_info where create_date between to_date('2015-07-01','YYYY-MM-DD') and to_date('2015-08-15','YYYY-MM-DD');

--------------------- 本文来自 qq_17247301 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_17247301/article/details/80647520?utm_source=copy

二、查询近几天的数据

1.当前时间向前推一天\

SELECT current_timestamp - interval '1 day'

例:求出最近三天的数据
select * from 表名 where date between (SELECT current_timestamp - interval '3 day') and current_timestamp

2.当前时间向前推进一个月
SELECT current_timestamp - interval '1 month'

3.当前时间向前推进一年

SELECT current_timestamp - interval '1 year'
4.当前时间向前推一小时:
SELECT current_timestamp - interval '1 hour'
5.当前时间向前推一分钟:
SELECT current_timestamp - interval '1 min'
6.当前时间向前推60秒:
SELECT current_timestamp - interval '60 second'

猜你喜欢

转载自blog.csdn.net/menghuannvxia/article/details/82887448