I have read this question but it is not really helping me. I have a similar system of booking for homes. So everytime somebody needs to rent a house, he just logs on and select a time where the house he wants is free, and books it. So in my case, I also have a start_time and end_time. On my records I have precised time (datetime format) for each date so I can calculate date differences based on time precicion. Here is a sample of my database :
CREATE TABLE IF NOT EXISTS `bookings` (
`id_booking` int(11) NOT NULL AUTO_INCREMENT,
`id_user_staff` int(11) NOT NULL,
`start_date` datetime NOT NULL,
`end_date` datetime NOT NULL,
`details` text,
`statut` varchar(45) DEFAULT NULL,
`id_house` int(1) NOT NULL,
`fees` decimal(6,0) DEFAULT NULL,
`ip_user` varchar(15) NOT NULL,
`date_booked` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_booking`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `users` (
`id_user` int(5) NOT NULL DEFAULT '0',
`Last_Name` varchar(23) DEFAULT NULL,
`First_Name` varchar(23) DEFAULT NULL,
`Job_Title` varchar(56) DEFAULT NULL,
`Status` varchar(19) DEFAULT NULL,
`Company` varchar(38) DEFAULT NULL,
`Mobile` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `house` (
`id_house` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`details` text,
PRIMARY KEY (`id_house`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
My need now, is to determine how occupied is a home over a year, a precision based on number of days included in bookings for that home. That percentage is a ratio base on the number of days of booking over the number of days in the year. What will be my query to get the number of days and trying to avoid the issue of the similar question which count twice some days?