postgresql 日期查詢最全整理
1、獲取當(dāng)前日期
select now();

select current_timestamp;

返回值均是當(dāng)前年月日、時(shí)分秒,且秒保留6位小數(shù),兩種方式等價(jià)
select current_time;

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

返回值:年月日
2、查詢今天的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時(shí)間字段 >= current_date AND 時(shí)間字段 < current_date + 1;
3、查詢昨天的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時(shí)間字段 >= current_date - 1 AND 時(shí)間字段 < current_date;
4、查詢一個(gè)月內(nèi)的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時(shí)間字段 >= current_date - interval '1 month' AND 時(shí)間字段 <= current_date;
5、按日, 周, 月, 季度, 年統(tǒng)計(jì)數(shù)據(jù)
select date_trunc('DAY', 時(shí)間字段) as statisticTime, 分組字段, count(0) from 表名 GROUP BY date_trunc('DAY', 時(shí)間字段), 分組字段日: DAY; 周: WEEK; 月: MONTH; 季度: QUARTER; 年: YEAR
6、 查詢昨天、上周、上月、上年的日期
select to_char( now() - interval '1 day','yyyy-mm-dd'); select to_char( now() - interval '1 week','yyyy-mm-dd hh:mi:ss'); select to_char( now() - interval '1 month','yyyy-mm-dd'); select to_char( now() - interval '1 year','yyyy-mm-dd');
7、查詢今天、今月、今年的開始的日期時(shí)間
select date_trunc('year', now())
select date_trunc('month', now())
select date_trunc('day', now())
select date_trunc('hour', now())
select date_trunc('minute', now())
select date_trunc('second', now())8、查詢最近1秒,1分,1小時(shí),1天,1周(7天),1月,1年的記錄
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 seconds ' select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 minutes' select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 hours' select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 day' select * from 表名 where timestamp_start >= current_timestamp - interval ' 7 day' select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 month' select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 year'
9、從時(shí)間戳中提取 年月日時(shí)分秒、周
select date_part('year', timestamp '2024-02-16 12:38:40')
select date_part('month', timestamp '2024-02-16 12:38:40')
select date_part('day', timestamp '2024-02-16 12:38:40')
select date_part('hour', timestamp '2024-02-16 12:38:40')
select date_part('minute', timestamp '2024-02-16 12:38:40')
select date_part('second', timestamp '2024-02-16 12:38:40')
select date_part('week', timestamp '2024-02-16 12:38:40')到此這篇關(guān)于postgresql 您要的日期查詢都在這的文章就介紹到這了,更多相關(guān)postgresql 日期查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫
這篇文章主要介紹了如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02
postgresql數(shù)據(jù)庫連接數(shù)和狀態(tài)查詢操作
這篇文章主要介紹了postgresql數(shù)據(jù)庫連接數(shù)和狀態(tài)查詢操作,具有很好的參考價(jià)值,對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
使用python-slim鏡像遇到無法使用PostgreSQL的問題及解決方法
這篇文章主要介紹了使用python-slim鏡像遇到無法使用PostgreSQL的問題及解決方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
PostgreSQL 數(shù)據(jù)同步到ES 搭建操作
這篇文章主要介紹了PostgreSQL 數(shù)據(jù)同步到ES 搭建操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL 中的postgres_fdw擴(kuò)展詳解
這篇文章主要介紹了PostgreSQL 中的postgres_fdw擴(kuò)展詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL遷移的幾種實(shí)現(xiàn)方式
本文主要介紹了PostgreSQL遷移的幾種實(shí)現(xiàn)方式,包括邏輯備份、物理復(fù)制、文件系統(tǒng)快照及邏輯復(fù)制這四種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2025-06-06
PostgreSQL的擴(kuò)展dict_int應(yīng)用案例解析
dict_int擴(kuò)展為PostgreSQL提供了專業(yè)的整數(shù)文本處理能力,特別適合需要精確處理數(shù)字內(nèi)容的搜索場(chǎng)景,本文給大家介紹PostgreSQL的擴(kuò)展dict_int實(shí)際應(yīng)用案例,感興趣的朋友一起看看吧2025-07-07
PostgreSQL有效地處理數(shù)據(jù)序列化和反序列化的方法
在 PostgreSQL 中,處理數(shù)據(jù)的序列化和反序列化是確保數(shù)據(jù)在存儲(chǔ)、傳輸和處理過程中的一致性和可用性的重要任務(wù),這涉及到選擇合適的數(shù)據(jù)類型、轉(zhuǎn)換函數(shù)以及在應(yīng)用程序與數(shù)據(jù)庫之間進(jìn)行數(shù)據(jù)交互的策略,需要的朋友可以參考下2024-07-07
postgresql 13.1 insert into select并行查詢的實(shí)現(xiàn)
這篇文章主要介紹了解決postgresql insert into select無法使用并行查詢的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01

