postgresql 查询昨天的数据

版权声明: https://blog.csdn.net/qq_32157851/article/details/88852386

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';

或者(这里面的createtime是varchar 数据类型,数据库中是形如  '2019-03-26 16:30:30'这样

 select    * from tbl_tables  as n where age(current_date,to_timestamp(substring(n.createtime FROM 1 FOR 10),'yyyy-MM-dd')) ='1 days';

方法2:

 select	*
	from 表名 as n
	where  n.create_date>=current_date-1;

注:要使用方法2,那么表中的create_date 字段类型是 timestamp才可以

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
 

猜你喜欢

转载自blog.csdn.net/qq_32157851/article/details/88852386