当前位置:网站首页>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
边栏推荐
猜你喜欢

JS Deobfuscation - AST Restoration Case

一本通2074:【21CSPJ普及组】分糖果(candy)

Common commands and technical function modules of Metasploit

阿里云架构师金云龙:基于云XR平台的视觉计算应用部署

POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview

Metasploit常用命令、技术功能模块

电脑系统重装后怎么用打印机扫描出文件?

重装系统后新建文本文档打不开怎么办

nvm下node安装;node环境变量配置

Flask's routing (app.route) detailed
随机推荐
基于ABP的AppUser对象扩展
Multiple reasons for MySQL slow query
Rust dereference
Under the NVM node installation;The node environment variable configuration
18-GuliMall 压力测试与性能监控
从产品角度看 L2 应用:为什么说这是一个游乐场?
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
D. Binary String To Subsequences
电脑系统重装后怎么用打印机扫描出文件?
Postgresql源码(68)virtualxid锁的原理和应用场景
【EF】数据表全部字段更新与部分字段更新
Metasploit常用命令、技术功能模块
在“企业通讯录”的盲区,融云的边界与分寸
JSON 基本使用
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
Sudoku | Backtrack-7
The round functions in the np, ceil function and floor function
级联下拉菜单的实现「建议收藏」
TRUNCATE表之后空间未释放
The overall construction process of the Tensorflow model