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;
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/20211018/399918164c9086539e47b1869fecb7f7.gif)
返回值:當(dāng)前年月日、時(shí)分秒,且秒保留6位小數(shù)。
2.current_timestamp
![](/d/20211018/9d150fbbf30115ab39938cd759b3734c.gif)
返回值:當(dāng)前年月日、時(shí)分秒,且秒保留6位小數(shù)。(同上)
申明:now和current_timestamp幾乎沒(méi)區(qū)別,返回值相同,建議用now。
3.current_time
![](/d/20211018/c94097d25acae6528692bb922dd4d74a.gif)
返回值:時(shí)分秒,秒最高精確到6位
4.current_date
![](/d/20211018/68c24b8fbcf3234d73a8aeb323c5daea.gif)
返回值:年月日
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- pgsql 變量賦值方法及注意事項(xiàng)
- pgsql 實(shí)現(xiàn)分頁(yè)查詢方式
- Postgresql 存儲(chǔ)過(guò)程(plpgsql)兩層for循環(huán)的操作
- pgsql之create user與create role的區(qū)別介紹
- pgsql之pg_stat_replication的使用詳解
- pgsql 如何刪除仍有活動(dòng)鏈接的數(shù)據(jù)庫(kù)
- pgsql 解決包含有單引號(hào)的字符串操作