当前位置:网站首页>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
边栏推荐
- 用户代码未处理MetadataException
- 2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- Multiple reasons for MySQL slow query
- leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
- 面试官:Redis 大 key 要如何处理?
- Solution: Edu Codeforces 109 (div2)
- nvm下node安装;node环境变量配置
- Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
- The round functions in the np, ceil function and floor function
猜你喜欢

力扣 1413. 逐步求和得到正数的最小值

关于ETL的两种架构(ETL架构和ELT架构)

Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)

Presto Event Listener开发

In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture

从产品角度看 L2 应用:为什么说这是一个游乐场?

Basic JSON usage

The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition

Converting angles to radians

华为鸿蒙3.0的野望:技术、应用、生态
随机推荐
String hashing (2014 SERC J question)
好未来,想成为第二个新东方
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
为什么这么多人都想当产品经理?
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
工作经验-组件封装(拖拽排序组件)
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
json case
十步以内,用小程序快速生成App!
Basic JSON usage
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
从产品角度看 L2 应用:为什么说这是一个游乐场?
Blender程序化建模简明教程【PCG】
Bean life cycle
孙正义亏掉1500亿:当初投贵了
R语言拟合ARIMA模型并使用拟合模型进行预测推理:使用forecast函数计算ARIMA模型未来值(包含时间点、预测值、两个置信区间)
README_Albumentations
JSON 基本使用
Install win virtual machine on VMware
任务流执行器是如何工作的?