Mysql时间操作

一、MySql时间戳转换

sql 复制代码
select unix_timestamp();  #获取时间戳格式时间
select FROM_UNIXTIME(1717399499); #将时间戳转换为普通格式时间

二、Mysql时间相加减结果转换为秒

方法1:time_to_sec(timediff(endTime, startTime))

sql 复制代码
SELECT
  DISTINCT(column1),
  min(last_modified_time),
  max(last_modified_time),
  time_to_sec(
    timediff(max(last_modified_time), min(last_modified_time))
  ) as time1
FROM
  table1
where
  column1 = 'aaa';

方法2:timestampdiff(second, startTime, endTime)

sql 复制代码
SELECT
  DISTINCT(column1),
  min(last_modified_time),
  max(last_modified_time),
  timestampdiff(
    second,
    min(last_modified_time),
    max(last_modified_time)
  ) as time2
FROM
  table1
where
  column1 = 'aaa';

方法3:unix_timestamp(endTime) -unix_timestamp(startTime)

sql 复制代码
SELECT
  DISTINCT(column1),
  min(last_modified_time),
  max(last_modified_time),
  unix_timestamp(max(last_modified_time)) - unix_timestamp(min(last_modified_time)) as time3
FROM
  table1
where
  column1 = 'aaa';

三、Mysql时间相加减结果为时分秒

方法:timediff(time1,time2)

sql 复制代码
SELECT
  column1,
  max(last_modified_time),
  min(last_modified_time),
    timediff(
      max(last_modified_time),
      min(last_modified_time)
  )
FROM
  table1
where
  column1 = 'aaa';
相关推荐
武子康19 小时前
Java-143 深入浅出 MongoDB NoSQL:MongoDB、Redis、HBase、Neo4j应用场景与对比
java·数据库·redis·mongodb·性能优化·nosql·hbase
豆沙沙包?20 小时前
2025年--Lc171--H175 .组合两个表(SQL)
数据库·sql
麋鹿原20 小时前
Android Room 数据库之数据库升级
数据库·kotlin
源码集结号21 小时前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
GanGuaGua1 天前
MySQL:表的约束
数据库·mysql
Li zlun1 天前
MySQL 性能监控与安全管理完全指南
数据库·mysql·安全
养生技术人1 天前
Oracle OCP认证考试题目详解082系列第48题
运维·数据库·sql·oracle·database·开闭原则·ocp
海阳宜家电脑1 天前
Lazarus使用TSQLQuery更新的一点技巧
数据库·lazarus·tsqlquery
丨我是张先生丨1 天前
SQLSERVER 查找存储过程中某个变量
数据库
感谢地心引力1 天前
【Python】基于 PyQt6 和 Conda 的 PyInstaller 打包工具
数据库·python·conda·pyqt·pyinstaller