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

发布时间:2022-04-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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;

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

补充:postgresql 查询当前时间

需求:PostgreSQL中有四种获取当前时间的方式。

解决方案

1.now()

返回值:当前年月日、时分秒,且秒保留6位小数。

2.current_timestamp

返回值:当前年月日、时分秒,且秒保留6位小数。(同上)

申明:now和current_timestamp几乎没区别,返回值相同,建议用now。

3.current_time

返回值:时分秒,秒最高精确到6位

4.current_date

返回值:年月日

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本宝典。如有错误或未考虑完全的地方,望不吝赐教。

脚本宝典总结

以上是脚本宝典为你收集整理的PGSQL 实现查询今天,昨天的数据,一个月之内的数据全部内容,希望文章能够帮你解决PGSQL 实现查询今天,昨天的数据,一个月之内的数据所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。