0

Hello i trying to select result date from mysql
mytable:

id_room | check_in | check_out


The problem is when i want to check reserved data betwwan date 1 to date 2.
for example my room data:

id_room : 1 ; check_in : 2014/11/01 ; check_out : 2014/11/08

So i want to check my booking in, 2014/11/03 and 2014/11/04. In system its will be not resulted thats mean no booking in that date, but in actual life 2014/11/03 and 2014/11/04 must have been booked. Because im check_in in 2014/11/01 and check_out in 2014/11/08.

I'm using

SELECT * FROM tabel WHERE cek_in BETWEEN '2014/11/03' AND '2014/11/04' OR cek_out BETWEEN 2014/11/03' AND '2014/11/04'

So if there any clue or other logic, please please tell me :D Thanks

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
febriyanmf
  • 23
  • 1
  • 6
  • 1
    Are the `check_in` and `check_out` columns actual date columns, or are they just storing strings? Also I'm sorry but I don't know what you're asking. – Andrew Nov 25 '14 at 14:16
  • @Andrew , i want create script to check booking date between date1 and date2. Customer can check empty room by searching time period – febriyanmf Nov 26 '14 at 02:09

1 Answers1

0

If I understand correctly; this is a classic problem you want to include bookings that overlap the dates:

SELECT * 
  FROM table1
 WHERE cek_out >= '2014-11-03' /* Check OUT >= START of range to check */
   AND cek_in  <= '2014-11-05' /* Check IN  <=  END  of range to check */;

UPDATE

So you are running the query above and the room has an existing booking

cek_in  = '2014-11-01' (<= '2014-11-05') /* OK */
cek_out = '2014-11-05' (>= '2014-11-03') /* OK */

This should show up, are you sure you wrote the correct query? See comments.

Arth
  • 12,789
  • 5
  • 37
  • 69
  • thx for the solution. Im trying to add your sql, but when i check for date '2014-11-03' and '2014-11-05', MySQL gives empty result. In actually, Room1 has already booked for date 01 to date 05. Im so sorry for bad description – febriyanmf Nov 26 '14 at 02:19