postgresql 日期查詢最全整理
1、獲取當(dāng)前日期
select now();
select current_timestamp;
返回值均是當(dāng)前年月日、時分秒,且秒保留6位小數(shù),兩種方式等價
select current_time;
返回值:時分秒,秒最高精確到6位
select current_date;
返回值:年月日
2、查詢今天的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時間字段 >= current_date AND 時間字段 < current_date + 1;
3、查詢昨天的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時間字段 >= current_date - 1 AND 時間字段 < current_date;
4、查詢一個月內(nèi)的數(shù)據(jù)
SELECT * FROM 表名 WHERE 時間字段 >= current_date - interval '1 month' AND 時間字段 <= current_date;
5、按日, 周, 月, 季度, 年統(tǒng)計數(shù)據(jù)
select date_trunc('DAY', 時間字段) as statisticTime, 分組字段, count(0) from 表名 GROUP BY date_trunc('DAY', 時間字段), 分組字段
日: 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、查詢今天、今月、今年的開始的日期時間
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小時,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、從時間戳中提取 年月日時分秒、周
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PostgreSQL中數(shù)據(jù)批量導(dǎo)入導(dǎo)出的錯誤處理
在 PostgreSQL 中進(jìn)行數(shù)據(jù)的批量導(dǎo)入導(dǎo)出是常見的操作,但有時可能會遇到各種錯誤,下面將詳細(xì)探討可能出現(xiàn)的錯誤類型、原因及相應(yīng)的解決方案,并提供具體的示例來幫助您更好地理解和處理這些問題,需要的朋友可以參考下2024-07-07postgres 使用存儲過程批量插入數(shù)據(jù)的操作
這篇文章主要介紹了postgres 使用存儲過程批量插入數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02postgresql 中的加密擴(kuò)展插件pgcrypto用法說明
這篇文章主要介紹了postgresql 中的加密擴(kuò)展插件pgcrypto用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01postgresql?IvorySQL新增命令及相關(guān)配置參數(shù)詳解
這篇文章主要為大家介紹了postgresql?IvorySQL新增命令及相關(guān)配置參數(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Ruoyi從mysql切換到postgresql的幾個踩坑實戰(zhàn)
最近由于工作的原因,需要將Ruoyi從mysql切換到postgresql,所以這篇文章主要給大家介紹了關(guān)于Ruoyi從mysql切換到postgresql的幾個踩坑實戰(zhàn),需要的朋友可以參考下2023-02-02本地計算機(jī)上的 postgresql 服務(wù)啟動后停止的問題解決
這篇文章主要介紹了本地計算機(jī)上的 postgresql 服務(wù)啟動后停止的問題解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01