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';
相关推荐
poemyang2 小时前
十年大厂员工终明白:MySQL性能优化的尽头,是对B+树的极致理解
mysql·pagecache·顺序i/o·局部性原理·b tree·b+ tree
wyiyiyi3 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
天宇_任3 小时前
Mysql数据库迁移到GaussDB注意事项
数据库·mysql·gaussdb
xiep14383335106 小时前
Ubuntu 安装带证书的 etcd 集群
数据库·etcd
Java小白程序员7 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
老虎06277 小时前
数据库基础—SQL语句总结及在开发时
数据库·sql·oracle
爱掘金的土拨鼠9 小时前
国产化dm数据库锁表解锁
数据库
花花无缺9 小时前
MySQL 的存储引擎-InnoDB 和 MyISAM的对比
mysql
苏琢玉9 小时前
如何让同事自己查数据?写一个零依赖 PHP SQL 查询工具就够了
mysql·php
庖丁解java10 小时前
N个Utils
数据库