MySql query to find rooms available given dates 'From' and 'to'
Tables are as follows
rooms(RoomId(PK)(int),
RoomName(varchar))
bookings(RoomId(FK)(int),
From_D(date),
To_D(date),
Bookee(varchar))
(PK-primary Key ,FK-foreign Key)
Given the arrival(From_D)
and departure(To_D)
dates how do i find out the id of the rooms that are available
I tried:
SELECT DISTINCT RoomId
FROM bookings
WHERE arrival not in(From_D,To_d) AND
departure not in(From_D,To_d)
But seeing the structure of my DB problem occurs if there multiple booking of a room stored in the DB, and it books the room even if it was not free in that range of dates
mysql sql
add a comment |
Tables are as follows
rooms(RoomId(PK)(int),
RoomName(varchar))
bookings(RoomId(FK)(int),
From_D(date),
To_D(date),
Bookee(varchar))
(PK-primary Key ,FK-foreign Key)
Given the arrival(From_D)
and departure(To_D)
dates how do i find out the id of the rooms that are available
I tried:
SELECT DISTINCT RoomId
FROM bookings
WHERE arrival not in(From_D,To_d) AND
departure not in(From_D,To_d)
But seeing the structure of my DB problem occurs if there multiple booking of a room stored in the DB, and it books the room even if it was not free in that range of dates
mysql sql
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49
add a comment |
Tables are as follows
rooms(RoomId(PK)(int),
RoomName(varchar))
bookings(RoomId(FK)(int),
From_D(date),
To_D(date),
Bookee(varchar))
(PK-primary Key ,FK-foreign Key)
Given the arrival(From_D)
and departure(To_D)
dates how do i find out the id of the rooms that are available
I tried:
SELECT DISTINCT RoomId
FROM bookings
WHERE arrival not in(From_D,To_d) AND
departure not in(From_D,To_d)
But seeing the structure of my DB problem occurs if there multiple booking of a room stored in the DB, and it books the room even if it was not free in that range of dates
mysql sql
Tables are as follows
rooms(RoomId(PK)(int),
RoomName(varchar))
bookings(RoomId(FK)(int),
From_D(date),
To_D(date),
Bookee(varchar))
(PK-primary Key ,FK-foreign Key)
Given the arrival(From_D)
and departure(To_D)
dates how do i find out the id of the rooms that are available
I tried:
SELECT DISTINCT RoomId
FROM bookings
WHERE arrival not in(From_D,To_d) AND
departure not in(From_D,To_d)
But seeing the structure of my DB problem occurs if there multiple booking of a room stored in the DB, and it books the room even if it was not free in that range of dates
mysql sql
mysql sql
edited Nov 15 '18 at 11:56
Madhur Bhaiya
19.6k62236
19.6k62236
asked Nov 15 '18 at 6:46
Tanmay ShrivastavaTanmay Shrivastava
84
84
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49
add a comment |
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49
add a comment |
4 Answers
4
active
oldest
votes
Consider the following diagram (sorry for bad handwriting):
In the above diagram, we can see date ranges on a horizontal axis. From_D
to To_D
represents a booked slot for a specific room. Four cases are considered for Arrival and Departure dates (denoted by Ai and Di).
We can clearly see that a particular room is available only when the arrival and departure dates are either completely on the left side, or the right side of the booked slot.
We can GROUP BY
on RoomID
and consider only those rooms, where every booking for follows the criteria defined above, using the HAVING
clause.
The query would be simply the following:
SELECT RoomID
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < :arrival AND To_D < :arrival)
OR
(From_D > :departure AND To_D > :departure)) = COUNT(*)
Demo on DB Fiddle
CREATE TABLE `rooms` (
`RoomId` int(4) NOT NULL,
`RoomName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `bookings` (
`RoomId` int(4) NOT NULL,
`From_D` date NOT NULL,
`To_d` date NOT NULL,
`B_Name` varchar(20) NOT NULL,
KEY `RoomId` (`RoomId`),
CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES
('1', 'Auditorium'),
('2', 'Room2'),
('3', 'Room3'),
('4', 'Room4'),
('5', 'Room5');
INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES
('1', '2018-11-01', '2018-11-03', 'Trance'),
('2', '2018-11-02', '2018-11-07', 'Alcoding'),
('3', '2018-11-01', '2018-11-04', 'DebSoc'),
('4', '2018-11-12', '2018-11-17', 'MunSoc'),
('5', '2018-11-03', '2018-11-06', 'Pulse');
Query: Check for any availability between 2018-11-01 and 2018-11-03
SELECT RoomId
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < '2018-11-01' AND To_D < '2018-11-01')
OR
(From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)
Result: Only RoomId 4 is available as per the sample data
| RoomId |
| ------ |
| 4 |
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
|
show 6 more comments
Use BETWEEN and AND with your date
SELECT DISTINCT RoomId FROM bookings
WHERE (date_field BETWEEN From_D AND To_d)
add a comment |
select distinct r.*
from rooms r
left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should@arrival
do? That does not look like valid SQL
– Nico Haase
Nov 15 '18 at 17:25
add a comment |
Try this :
SELECT * FROM rooms room1
WHERE NOT EXISTS (SELECT *
FROM booking booking1
WHERE room1.roomid = booking1.roomid AND
(:from_d BETWEEN booking1.from_d AND booking1.to_d OR
:to_d BETWEEN booking1.from_d AND booking1.to_d))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313840%2fmysql-query-to-find-rooms-available-given-dates-from-and-to%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Consider the following diagram (sorry for bad handwriting):
In the above diagram, we can see date ranges on a horizontal axis. From_D
to To_D
represents a booked slot for a specific room. Four cases are considered for Arrival and Departure dates (denoted by Ai and Di).
We can clearly see that a particular room is available only when the arrival and departure dates are either completely on the left side, or the right side of the booked slot.
We can GROUP BY
on RoomID
and consider only those rooms, where every booking for follows the criteria defined above, using the HAVING
clause.
The query would be simply the following:
SELECT RoomID
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < :arrival AND To_D < :arrival)
OR
(From_D > :departure AND To_D > :departure)) = COUNT(*)
Demo on DB Fiddle
CREATE TABLE `rooms` (
`RoomId` int(4) NOT NULL,
`RoomName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `bookings` (
`RoomId` int(4) NOT NULL,
`From_D` date NOT NULL,
`To_d` date NOT NULL,
`B_Name` varchar(20) NOT NULL,
KEY `RoomId` (`RoomId`),
CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES
('1', 'Auditorium'),
('2', 'Room2'),
('3', 'Room3'),
('4', 'Room4'),
('5', 'Room5');
INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES
('1', '2018-11-01', '2018-11-03', 'Trance'),
('2', '2018-11-02', '2018-11-07', 'Alcoding'),
('3', '2018-11-01', '2018-11-04', 'DebSoc'),
('4', '2018-11-12', '2018-11-17', 'MunSoc'),
('5', '2018-11-03', '2018-11-06', 'Pulse');
Query: Check for any availability between 2018-11-01 and 2018-11-03
SELECT RoomId
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < '2018-11-01' AND To_D < '2018-11-01')
OR
(From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)
Result: Only RoomId 4 is available as per the sample data
| RoomId |
| ------ |
| 4 |
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
|
show 6 more comments
Consider the following diagram (sorry for bad handwriting):
In the above diagram, we can see date ranges on a horizontal axis. From_D
to To_D
represents a booked slot for a specific room. Four cases are considered for Arrival and Departure dates (denoted by Ai and Di).
We can clearly see that a particular room is available only when the arrival and departure dates are either completely on the left side, or the right side of the booked slot.
We can GROUP BY
on RoomID
and consider only those rooms, where every booking for follows the criteria defined above, using the HAVING
clause.
The query would be simply the following:
SELECT RoomID
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < :arrival AND To_D < :arrival)
OR
(From_D > :departure AND To_D > :departure)) = COUNT(*)
Demo on DB Fiddle
CREATE TABLE `rooms` (
`RoomId` int(4) NOT NULL,
`RoomName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `bookings` (
`RoomId` int(4) NOT NULL,
`From_D` date NOT NULL,
`To_d` date NOT NULL,
`B_Name` varchar(20) NOT NULL,
KEY `RoomId` (`RoomId`),
CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES
('1', 'Auditorium'),
('2', 'Room2'),
('3', 'Room3'),
('4', 'Room4'),
('5', 'Room5');
INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES
('1', '2018-11-01', '2018-11-03', 'Trance'),
('2', '2018-11-02', '2018-11-07', 'Alcoding'),
('3', '2018-11-01', '2018-11-04', 'DebSoc'),
('4', '2018-11-12', '2018-11-17', 'MunSoc'),
('5', '2018-11-03', '2018-11-06', 'Pulse');
Query: Check for any availability between 2018-11-01 and 2018-11-03
SELECT RoomId
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < '2018-11-01' AND To_D < '2018-11-01')
OR
(From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)
Result: Only RoomId 4 is available as per the sample data
| RoomId |
| ------ |
| 4 |
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
|
show 6 more comments
Consider the following diagram (sorry for bad handwriting):
In the above diagram, we can see date ranges on a horizontal axis. From_D
to To_D
represents a booked slot for a specific room. Four cases are considered for Arrival and Departure dates (denoted by Ai and Di).
We can clearly see that a particular room is available only when the arrival and departure dates are either completely on the left side, or the right side of the booked slot.
We can GROUP BY
on RoomID
and consider only those rooms, where every booking for follows the criteria defined above, using the HAVING
clause.
The query would be simply the following:
SELECT RoomID
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < :arrival AND To_D < :arrival)
OR
(From_D > :departure AND To_D > :departure)) = COUNT(*)
Demo on DB Fiddle
CREATE TABLE `rooms` (
`RoomId` int(4) NOT NULL,
`RoomName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `bookings` (
`RoomId` int(4) NOT NULL,
`From_D` date NOT NULL,
`To_d` date NOT NULL,
`B_Name` varchar(20) NOT NULL,
KEY `RoomId` (`RoomId`),
CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES
('1', 'Auditorium'),
('2', 'Room2'),
('3', 'Room3'),
('4', 'Room4'),
('5', 'Room5');
INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES
('1', '2018-11-01', '2018-11-03', 'Trance'),
('2', '2018-11-02', '2018-11-07', 'Alcoding'),
('3', '2018-11-01', '2018-11-04', 'DebSoc'),
('4', '2018-11-12', '2018-11-17', 'MunSoc'),
('5', '2018-11-03', '2018-11-06', 'Pulse');
Query: Check for any availability between 2018-11-01 and 2018-11-03
SELECT RoomId
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < '2018-11-01' AND To_D < '2018-11-01')
OR
(From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)
Result: Only RoomId 4 is available as per the sample data
| RoomId |
| ------ |
| 4 |
Consider the following diagram (sorry for bad handwriting):
In the above diagram, we can see date ranges on a horizontal axis. From_D
to To_D
represents a booked slot for a specific room. Four cases are considered for Arrival and Departure dates (denoted by Ai and Di).
We can clearly see that a particular room is available only when the arrival and departure dates are either completely on the left side, or the right side of the booked slot.
We can GROUP BY
on RoomID
and consider only those rooms, where every booking for follows the criteria defined above, using the HAVING
clause.
The query would be simply the following:
SELECT RoomID
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < :arrival AND To_D < :arrival)
OR
(From_D > :departure AND To_D > :departure)) = COUNT(*)
Demo on DB Fiddle
CREATE TABLE `rooms` (
`RoomId` int(4) NOT NULL,
`RoomName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `bookings` (
`RoomId` int(4) NOT NULL,
`From_D` date NOT NULL,
`To_d` date NOT NULL,
`B_Name` varchar(20) NOT NULL,
KEY `RoomId` (`RoomId`),
CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES
('1', 'Auditorium'),
('2', 'Room2'),
('3', 'Room3'),
('4', 'Room4'),
('5', 'Room5');
INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES
('1', '2018-11-01', '2018-11-03', 'Trance'),
('2', '2018-11-02', '2018-11-07', 'Alcoding'),
('3', '2018-11-01', '2018-11-04', 'DebSoc'),
('4', '2018-11-12', '2018-11-17', 'MunSoc'),
('5', '2018-11-03', '2018-11-06', 'Pulse');
Query: Check for any availability between 2018-11-01 and 2018-11-03
SELECT RoomId
FROM bookings
GROUP BY RoomID
HAVING
SUM((From_D < '2018-11-01' AND To_D < '2018-11-01')
OR
(From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)
Result: Only RoomId 4 is available as per the sample data
| RoomId |
| ------ |
| 4 |
edited Nov 22 '18 at 20:21
answered Nov 15 '18 at 12:19
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
|
show 6 more comments
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
I tried running this query but it returns zero rows for dates in which some rooms are free
– Tanmay Shrivastava
Nov 16 '18 at 18:42
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
@TanmayShrivastava can you please provide the SQL for the sample data in the table, in which this query is failing
– Madhur Bhaiya
Nov 16 '18 at 18:44
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'< b2.From_D AND '2018-11-03' < b2.From_D) OR NOT('2018-11-01' > b2.To_D AND '2018-11-03' > b2.To_D) )
– Tanmay Shrivastava
Nov 16 '18 at 18:49
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
i know that there is a room that is free between the dates given but the query returns zero results
– Tanmay Shrivastava
Nov 16 '18 at 18:51
1
1
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
Done ,I have set the data up
– Tanmay Shrivastava
Nov 16 '18 at 19:35
|
show 6 more comments
Use BETWEEN and AND with your date
SELECT DISTINCT RoomId FROM bookings
WHERE (date_field BETWEEN From_D AND To_d)
add a comment |
Use BETWEEN and AND with your date
SELECT DISTINCT RoomId FROM bookings
WHERE (date_field BETWEEN From_D AND To_d)
add a comment |
Use BETWEEN and AND with your date
SELECT DISTINCT RoomId FROM bookings
WHERE (date_field BETWEEN From_D AND To_d)
Use BETWEEN and AND with your date
SELECT DISTINCT RoomId FROM bookings
WHERE (date_field BETWEEN From_D AND To_d)
edited Nov 15 '18 at 6:54
answered Nov 15 '18 at 6:49
Sachin ShahSachin Shah
1,7561516
1,7561516
add a comment |
add a comment |
select distinct r.*
from rooms r
left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should@arrival
do? That does not look like valid SQL
– Nico Haase
Nov 15 '18 at 17:25
add a comment |
select distinct r.*
from rooms r
left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should@arrival
do? That does not look like valid SQL
– Nico Haase
Nov 15 '18 at 17:25
add a comment |
select distinct r.*
from rooms r
left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td
select distinct r.*
from rooms r
left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td
answered Nov 15 '18 at 16:40
Miguel Angel AlonsoMiguel Angel Alonso
364
364
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should@arrival
do? That does not look like valid SQL
– Nico Haase
Nov 15 '18 at 17:25
add a comment |
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should@arrival
do? That does not look like valid SQL
– Nico Haase
Nov 15 '18 at 17:25
1
1
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
answer with only code are poor, can you please explain what your code does?
– DaFois
Nov 15 '18 at 16:53
What should
@arrival
do? That does not look like valid SQL– Nico Haase
Nov 15 '18 at 17:25
What should
@arrival
do? That does not look like valid SQL– Nico Haase
Nov 15 '18 at 17:25
add a comment |
Try this :
SELECT * FROM rooms room1
WHERE NOT EXISTS (SELECT *
FROM booking booking1
WHERE room1.roomid = booking1.roomid AND
(:from_d BETWEEN booking1.from_d AND booking1.to_d OR
:to_d BETWEEN booking1.from_d AND booking1.to_d))
add a comment |
Try this :
SELECT * FROM rooms room1
WHERE NOT EXISTS (SELECT *
FROM booking booking1
WHERE room1.roomid = booking1.roomid AND
(:from_d BETWEEN booking1.from_d AND booking1.to_d OR
:to_d BETWEEN booking1.from_d AND booking1.to_d))
add a comment |
Try this :
SELECT * FROM rooms room1
WHERE NOT EXISTS (SELECT *
FROM booking booking1
WHERE room1.roomid = booking1.roomid AND
(:from_d BETWEEN booking1.from_d AND booking1.to_d OR
:to_d BETWEEN booking1.from_d AND booking1.to_d))
Try this :
SELECT * FROM rooms room1
WHERE NOT EXISTS (SELECT *
FROM booking booking1
WHERE room1.roomid = booking1.roomid AND
(:from_d BETWEEN booking1.from_d AND booking1.to_d OR
:to_d BETWEEN booking1.from_d AND booking1.to_d))
answered Nov 15 '18 at 7:36
Atul AkabariAtul Akabari
954
954
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313840%2fmysql-query-to-find-rooms-available-given-dates-from-and-to%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please provide sample data and your expected output in table format
– fa06
Nov 15 '18 at 6:49