Bootstrap

Mysql时间操作

一、MySql时间戳转换

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

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

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

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)

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)

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)

SELECT
  column1,
  max(last_modified_time),
  min(last_modified_time),
    timediff(
      max(last_modified_time),
      min(last_modified_time)
  )
FROM
  table1
where
  column1 = 'aaa';
;