从timin和timout表中获取总时数?

时间:2016-07-29 19:34:06

标签: mysql logic

我有这两个表,当用户登录时我将当前stimestamp存储到timein表中,其中字段为id int 11 auto inc,user_id和time我现在用于注销的方式我的问题是如何才能获得完全登录通过从timein和timeout表计算字段时间是当前时间戳的小时数?日和月明智

这就是我的架构的样子

CREATE TABLE IF NOT EXISTS `timein` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `member_name` varchar(32) NOT NULL,
  `team` varchar(32) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `timeout` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `member_name` varchar(32) NOT NULL,
  `team` varchar(32) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

1 个答案:

答案 0 :(得分:0)

用于计算小时使用timestampdiff功能

    select *,concat(timestampdiff(hour, a.time, b.time),' hours') from timeout b,timein a
where a.id=b.id

而不是仅在小时内显示,下面的查询可以按月,日和小时显示。

select 

if(timestampdiff(month,a.time,b.time)>0,concat(timestampdiff(month,a.time,b.time),' month'),

if(datediff(b.time,a.time)>0, concat(datediff(b.time,a.time),' day'),

concat(timestampdiff(hour,a.time,b.time),' hours')))time from timein a,timeout b

where b.id=a.id;