当前位置:网站首页>mysql 、pg 查询日期处理
mysql 、pg 查询日期处理
2022-08-09 21:55:00 【何xiao树】
mysql
日期格式化:
'%Y-%d-%m %H:%m:%s'
- 完整格式化字符串
-- 年月
select create_time,date_format(create_time,'%Y-%d') from tableName
-- 这截取查询出来是字符
select create_time,left(create_time,7) from tableName
-- 时分秒
select create_time,date_format(create_time,'%H:%m:%s') from tableName
select create_time,right(create_time,8) from tableName
-- 字符转日期
SELECT stat_time, str_to_date(stat_time, '%Y-%m-%d') as statDate FROM tableName
日期范围:
-- 查询 2小时前的数据
select * from tableName
where create_time > NOW()-INTERVAL '2' HOUR and create_time<=now();
-- 时间差
select TIMESTAMPDIFF(MINUTE,create_time,now())
from tableName
-- 查询本周数据
SELECT create_time FROM tableName WHERE YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now());
-- 上周
SELECT create_time FROM tableName WHERE YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())-1;
postgis
日期范围:
-- 查询 2小时前的数据
select * from tableName
where create_time >= now() + ' -2 Hours ' AND create_time <= now()
-- mybatis 中采用静态替换 如:' ${param.intervalTime} ' ,来表示 ' -2 Hours '
日期转换:
'yyyy-MM-dd hh24:mi:ss'
--完整格式
-- 时间戳转时间
select position_time,to_timestamp(position_time/1000) from tableName
-- 日期转字符
select position_time,to_char(to_timestamp(position_time/1000), 'yyyy-mm-dd') from tableName
-- 日期、时间分开存储时,日期转换
select date_str,time_str,to_timestamp( concat ( date_str, ' ', time_str ), 'yyyy-MM-dd hh24:mi:ss' ) from tableName
边栏推荐
猜你喜欢
Flask入门学习教程
Kubernetes Service对象
在“企业通讯录”的盲区,融云的边界与分寸
关于ETL的两种架构(ETL架构和ELT架构)
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
How do task flow executors work?
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
One Pass 2074: [21CSPJ Popularization Group] Candy
JS Deobfuscation - AST Restoration Case
Flask之路由(app.route)详解
随机推荐
大型分布式存储方案MinIO介绍,看完你就懂了!
台风生成,广州公交站场积极开展台风防御安全隐患排查
Usage of placeholder function in Tensorflow
【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
Interviewer: How to deal with Redis big key?
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
leetcode 38. 外观数列
unit test
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
SQLi-LABS Page-2 (Adv Injections)
SecureCRT sets the timeout period for automatic disconnection
Multiple reasons for MySQL slow query
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
js array object deduplication
关于ETL的两种架构(ETL架构和ELT架构)
用户代码未处理MetadataException
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
【测试】语句覆盖,判定覆盖,条件覆盖,路径覆盖
Leetcode.25 K个一组翻转链表(模拟/递归)
发送激活邮件「建议收藏」