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

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

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

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過

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

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 是一個(gè)timestamp的數(shù)據(jù);

current_date是pgsql數(shù)據(jù)一個(gè)獲取當(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)獲取兩個(gè)時(shí)間之差 返回 days

PG查詢最近一個(gè)月內(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';

 

補(bǔ)充:postgresql 查詢當(dāng)前時(shí)間

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

解決方案:

1.now()

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

2.current_timestamp

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

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

3.current_time

返回值:時(shí)分秒,秒最高精確到6位

4.current_date

返回值:年月日

文章來源:腳本之家

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

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門排行

信息推薦