PGSQL 实现查找今天 昨天的数据 一个月之内的数据

这篇文章主要介绍了PGSQL 实现查询今天,昨天的数据,一个月之内的数据,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。
 
PGSQL查询今天的数据
 
select *
 from 表名 as n
 where n.create_date>=current_date;
 
 
 
PG查询昨天的数据
 
方法1:
 
 select *
 from 表名 as n
 where
    age(
    current_date,to_timestamp(substring(to_char(n.create_date, 'yyyy-MM-dd hh24 : MI : ss' ) FROM 1 FOR 10),'yyyy-MM-dd')) ='1 days';
 
 
 
方法2:
 
select *
 from 表名 as n
 where n.create_date>=current_date-1 and n.create_date <current_date;
 
<current_date;< p="">
 
n.create_date 是一个timestamp的数据;
 
current_date是pgsql数据一个获取当前日期的字段;
 
to_char(timestamp,text)把timestamp数据转换成字符串;
 
substring(text from int for int) 截取想要的文本格式 ‘yyyy-MM-dd';
 
to_timestamp(text,'yyyy-MM-dd')转换成timestamp格式;
 
age(timestamp,timestamp)获取两个时间之差 返回 days
 
PG查询最近一个月内的数据
 
select *
 from 表名 as n
 and n.create_date>=to_timestamp(substring(to_char(now(),'yyyy-MM-dd hh24:MI:ss') FROM 1 FOR 10),'yyyy-MM-dd')- interval '30 day';
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章