当前位置:网站首页>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
边栏推荐
猜你喜欢
反射机制篇
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
华为鸿蒙3.0的野望:技术、应用、生态
[Microservice~Nacos] Nacos service provider and service consumer
关于ETL的两种架构(ETL架构和ELT架构)
【微服务~Nacos】Nacos之配置中心
How do task flow executors work?
C 在函数声明前加typedef
MLOps的演进历程
随机推荐
任务流执行器是如何工作的?
Deceptive Dice
Under the NVM node installation;The node environment variable configuration
abstract class or interface
Activiti7审批流
为什么这么多人都想当产品经理?
【EF】数据表全部字段更新与部分字段更新
【微服务~Nacos】Nacos服务提供者和服务消费者
Simple questions peek into mathematics
每日一R「02」所有权与 Move 语义
NodeJS使用JWT
Space not freed after TRUNCATE table
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
JS解混淆-AST还原案例
Solution: Edu Codeforces 109 (div2)
孙正义亏掉1500亿:当初投贵了
js十五道面试题(含答案)
反射机制篇
Cookie, session, token
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"