南通颐猩文化传播有限公司

當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

PGSQL 實現(xiàn)查詢今天 昨天的數(shù)據(jù) 一個月之內(nèi)的數(shù)據(jù)

 2021-05-21 16:56  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了PGSQL 實現(xiàn)查詢今天,昨天的數(shù)據(jù),一個月之內(nèi)的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。

PGSQL查詢今天的數(shù)據(jù)

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

 

PG查詢昨天的數(shù)據(jù)

方法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的數(shù)據(jù);

current_date是pgsql數(shù)據(jù)一個獲取當(dāng)前日期的字段;

to_char(timestamp,text)把timestamp數(shù)據(jù)轉(zhuǎn)換成字符串;

substring(text from int for int) 截取想要的文本格式 ‘yyyy-MM-dd';

to_timestamp(text,'yyyy-MM-dd')轉(zhuǎn)換成timestamp格式;

age(timestamp,timestamp)獲取兩個時間之差 返回 days

PG查詢最近一個月內(nèi)的數(shù)據(jù)

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 查詢當(dāng)前時間

需求:PostgreSQL中有四種獲取當(dāng)前時間的方式。

解決方案:

1.now()

返回值:當(dāng)前年月日、時分秒,且秒保留6位小數(shù)。

2.current_timestamp

返回值:當(dāng)前年月日、時分秒,且秒保留6位小數(shù)。(同上)

申明:now和current_timestamp幾乎沒區(qū)別,返回值相同,建議用now。

3.current_time

返回值:時分秒,秒最高精確到6位

4.current_date

返回值:年月日

文章來源:腳本之家

來源地址:https://www.jb51.net/article/204931.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關(guān)文章

熱門排行

信息推薦