简体   繁体   中英

mysql calculate total amount of days absent

How would I calculate the total amount of days a user has been absent within a given date period?

This is the basic idea of how I think it would work but I cant get the total part to work.

SELECT total(from - until) FROM absences WHERE absences.user_id = 123
AND absence_date_until <= '1999-12-01' AND absence_date_from >=
'2000-12-01'


CREATE TABLE IF NOT EXISTS `absences` (
  `absence_id` int(10) NOT NULL AUTO_INCREMENT,
  `absence_created` datetime DEFAULT NULL,
  `absence_date_from` datetime DEFAULT NULL,
  `absence_date_until` datetime DEFAULT NULL,
  `absence_status` enum('PENDING','APPROVED') NOT NULL,
  `user_id` int(6) NOT NULL,
  PRIMARY KEY (`absence_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
SELECT SUM(datediff(until, from) + 1) total_absences 
FROM   absences 
WHERE  absences.user_id = 123
       AND absence_date_until <= '1999-12-01' 
       AND absence_date_from  >= '2000-12-01'

select datediff() im mysql

SELECT datediff(absence_date_from- absence_date_until) 
FROM absences WHERE absences.user_id = 123
AND absence_date_until <= '1999-12-01' 
AND absence_date_from >=  '2000-12-01'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM