Reputation: 451
I am trying to develop a CAR renting system. Following are my DB table schema
CREATE TABLE IF NOT EXISTS `s_leases` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`vehicle_id` int(11) NOT NULL,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL
) ;
Sample Data
INSERT INTO `cs_leases`
(`id`, `vehicle_id`, `start_date` `end_date`) VALUES
(1, 1, '2018-03-07', '2018-03-12'),
(2, 1, '2018-03-17', '2018-03-21'),
(3, 1, '2018-03-24', '2018-03-30'),
(4, 3, '2018-03-07', '2018-03-10'),
(5, 3, '2018-03-12', '2018-03-15')
Above table contains car renting datils
CREATE TABLE IF NOT EXISTS `cs_lease_availabilities` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`lease_id` int(11) NOT NULL,
`vehicle_id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`start_time` varchar(25) DEFAULT NULL,
`end_time` varchar(25) DEFAULT NULL
);
Sample Data:
INSERT INTO `cs_lease_availabilities`
(`id`, `lease_id`,`vehicle_id`, `date`, `start_time`, `end_time`,`status`) VALUES
(1, 1, 1, '2018-03-07', '09:50 AM', '12:50 PM', 0),
(2, 1, 1, '2018-03-08', '01:51 PM', '04:50 PM',0),
(3, 1, 1, '2018-03-09', '01:06 PM', '10:00 PM',0),
(4, 1, 1, '2018-03-10', '09:00 AM', '10:00 AM', 0),
(5, 1, 1, '2018-03-11', '08:00 AM', '10:00 PM', 0),
(6, 1, 1, '2018-03-12', '12:00 PM', '10:00 PM',0),
(7,2, 1, '2018-03-17', '00:01 AM', '11:59 PM', 0),
(8, 2, 1, '2018-03-18', '00:01 AM', '11:59 PM', 0),
(9, 2, 1, '2018-03-19', '00:01 AM', '11:59 PM',0),
(10, 2, 1, '2018-03-20', '00:01 AM', '11:59 PM', 0),
(11, 2, 1, '2018-03-21', '00:01 AM', '11:59 PM',0),
(12, 3, 1, '2018-03-24', '00:01 AM', '11:59 PM',0),
(13, 3, 1, '2018-03-25', '00:01 AM', '11:59 PM', 0),
(14, 3, 1, '2018-03-26', '00:01 AM', '11:59 PM',0),
(15, 3, 1, '2018-03-27', '00:01 AM', '11:59 PM', 0),
(16, 3, 1, '2018-03-28', '00:01 AM', '11:59 PM',0),
(17, 3, 1, '2018-03-29', '00:01 AM', '11:59 PM', 0),
(18, 3, 1, '2018-03-30', '00:01 AM', '11:59 PM',0),
(19, 4, 3, '2018-03-07', '00:01 AM', '11:59 PM', 0),
(20, 4, 3, '2018-03-08', '00:01 AM', '11:59 PM', 0),
(21, 4, 3, '2018-03-09', '00:01 AM', '11:59 PM', 0),
(22, 4, 3, '2018-03-10', '00:01 AM', '11:59 PM', 0),
(23, 5, 3, '2018-03-12', '00:01 AM', '11:59 PM', 0),
(24, 5, 3, '2018-03-13', '00:01 AM', '11:59 PM', 0),
(25, 7, 3, '2018-03-14', '00:01 AM', '11:59 PM', 0),
(26, 7, 3, '2018-03-15', '08:01 AM', '11:00 PM', 0)
Above table contains the available time related info per day.
CREATE TABLE IF NOT EXISTS `cs_orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lease_id` int(11) NOT NULL,
`vehicle_id` int(11) NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`status` tinyint(2) NOT NULL DEFAULT '0'
);
This table save the booking related info
Following table save booking time per day info.
CREATE TABLE IF NOT EXISTS `cs_order_availabilities` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`lease_id` int(11) NOT NULL,
`lease_availability_id` int(11) NOT NULL,
`cs_order_id` int(11) NOT NULL,
`vehicle_id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`start_time` varchar(25) DEFAULT NULL,
`end_time` varchar(25) DEFAULT NULL
);
Now i am struggling to build a mysql query that can return available vehicle leases records between a date range with continuous date availability.
If i try to find availability date between 2018-03-08 to 2018-03-15, then no record should show (cs lease id 1 & 2 are not for continuous date).
Any help would be appreciated.
Upvotes: 0
Views: 1265
Reputation: 48387
Deciding on your schema before you've worked out how you are going to query the data might not be the best approach.
If it were me, I would use a single table to hold both the bookings and the availability of a particular resource.
Then it would simply be a matter of...
SELECT *
FROM resource_calendar
WHERE status='available'
AND start_time<$booking_start
AND end_time>$booking_end
ORDER BY unix_timestamp($booking_start)-unix_timestamp(start_time)
But there is no simple way to optimize any schema to use conventional indexes effectively. If you want fast queries then you'll need to use geo-spatial indexing (yes, that question talks about IP addresses but it's the same problem)
Upvotes: 0
Reputation: 1722
You can try something like ,
SET @st = '2018-03-07';
SET @et = '2018-03-09';
SELECT cs.lease_id, COUNT(cs.`date`) AS ct
FROM cs_lease_availabilities cs
WHERE cs.`date` BETWEEN @st AND @et
GROUP BY cs.lease_id HAVING ct = TIMESTAMPDIFF(DAY , @st , @et)+1;
I hope this will return your expected result.
Upvotes: 1