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';
相关推荐
GottdesKrieges20 小时前
OceanBase系统日志管理
数据库·oracle·oceanbase
小嵌同学1 天前
Linux:malloc背后的实现细节
大数据·linux·数据库
努力的小郑1 天前
MySQL索引(三):字符串索引优化之前缀索引
后端·mysql·性能优化
R瑾安1 天前
mysql安装(压缩包方式8.0及以上)
数据库·mysql
代码的余温1 天前
MySQL Cluster核心优缺点
数据库·mysql
Mr.Entropy1 天前
请求超过Spring线程池的最大线程(处理逻辑)
数据库·sql·spring
GBASE1 天前
“G”术时刻:南大通用GBase 8c数据库权限管理场景实践(二)
数据库
努力的小郑1 天前
MySQL索引(二):覆盖索引、最左前缀原则与索引下推详解
后端·mysql
wearegogog1231 天前
MySQL中实施排序(sorting)及分组(grouping)操作
数据库·mysql
知其然亦知其所以然1 天前
一条 SQL 的一生,从出生到谢幕,揭秘 MySQL8.x 内幕
后端·mysql·面试