postgresql 您要的日期查询都在这
创始人
2024-11-05 00:34:46
0

文章目录

    • 1、获取当前日期
    • 2、查询今天的数据
    • 3、查询昨天的数据
    • 4、查询一个月内的数据
    • 5、按日, 周, 月, 季度, 年统计数据
    • 6、 查询昨天、上周、上月、上年的日期
    • 7、查询今天、今月、今年的开始的日期时间
    • 8、查询最近1秒,1分,1小时,1天,1周(7天),1月,1年的记录
    • 9、从时间戳中提取 年月日时分秒、周


1、获取当前日期

select now(); 

在这里插入图片描述

select current_timestamp; 

在这里插入图片描述
返回值均是当前年月日、时分秒,且秒保留6位小数,两种方式等价

select current_time; 

在这里插入图片描述
返回值:时分秒,秒最高精确到6位

select current_date; 

在这里插入图片描述
返回值:年月日

2、查询今天的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date AND 时间字段 < current_date + 1; 

3、查询昨天的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date - 1 AND 时间字段 < current_date; 

4、查询一个月内的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date - interval '1 month' AND 时间字段 <= current_date; 

5、按日, 周, 月, 季度, 年统计数据

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') 

相关内容

热门资讯

不少玩家反映!hhpoker德... 不少玩家反映!hhpoker德州透视,悟空大厅辅助助手下载安装,烘培教程(有挂猫腻)1、完成悟空大厅...
2026版辅助挂!wepoke... 2026版辅助挂!wepoker俱乐部辅助,四川熊猫辅助官网,办法教程(有人有挂)1、四川熊猫辅助官...
截至发稿!wepoker开脚本... 截至发稿!wepoker开脚本视频,麻雀智能辅助软件开挂,机巧教程(有挂方式)1、游戏颠覆性的策略玩...
为切实保障!hhpoker辅助... 为切实保障!hhpoker辅助软件,微乐小程序辅助收费,妙计教程(有挂猫腻)微乐小程序辅助收费破解侠...
据公告内容!wepoker俱乐... 据公告内容!wepoker俱乐部辅助器,黑科技微乐小程序辅助器免费,秘籍教程(确实有挂)进入游戏-大...
连日来!wepokerplus... 连日来!wepokerplus万能挂,对战互娱辅助,机巧教程(有挂总结)1)对战互娱辅助辅助插件:进...
反观!aapoker公共底牌,... 反观!aapoker公共底牌,钱塘十水三攻略,教材教程(有挂解惑)1、进入游戏-大厅左侧-新手福利-...
据了解!werplan辅助软件... 据了解!werplan辅助软件,天天互娱辅助器免费下载,指南书教程(有挂实锤)1、天天互娱辅助器免费...
2026版攻略!佛手大菠萝辅助... 2026版攻略!佛手大菠萝辅助,天天互娱辅助器免费下载,手段教程(有挂神器)1、天天互娱辅助器免费下...
据玩家消息!德州真人透视脚本,... 据玩家消息!德州真人透视脚本,蜀山辅助工具,大纲教程(有挂解惑)1、下载好蜀山辅助工具脚本下载之后点...