Sql date search problem

I have a database in mysql for room availability in a hotel. I want to allow a search for room type and date availability.
I use the between function of sql but the problem is searching for multiple dates. I can search for single dates but for someone searching for a period e.g 1st Feb to 7th Feb , I get reduntant records showing up.
Below is the html code and I would like to know the sql code to select this data from the database. I hope this is clear and if anyone wants more information please let me know.




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 2</title>
</head>

<body>

<form method="POST" action="search.php3">
  <div align="center">
    <center>
    <table border="0" width="50%">
      <tr>
        <td width="33%" bgcolor="#CCCCCC">
          <p align="center"><input type="text" name="arrday" size="20"></td>
        <td width="37%" bgcolor="#CCCCCC"><select name="arrmonth" size="1" onChange="updatemonth(this.form)" class="BodySansSmall">
            <option>January</option>
            <option>February</option>
            <option>March</option>
            <option>April</option>
            <option>May</option>
            <option>June</option>
            <option>July</option>
            <option>August</option>
            <option>September</option>
            <option>October</option>
            <option>November</option>
            <option>December</option>
          </select></td>
        <td width="30%" bgcolor="#CCCCCC"><select name="arryear" size="1" onChange="updateyear(this.form)" class="BodySansSmall">
            <option>2000</option>
            <option>2001</option>
          </select></td>
      </tr>
      <tr>
        <td width="34%" bgcolor="#CCCCCC">
          <p align="center"><input type="text" name="endday" size="20"></td>
        <td width="33%" bgcolor="#CCCCCC"><select name="end_month" size="1" class="BodySansSmall">
            <option>January</option>
            <option>February</option>
            <option>March</option>
            <option>April</option>
            <option>May</option>
            <option>June</option>
            <option>July</option>
            <option>August</option>
            <option>September</option>
            <option>October</option>
            <option>November</option>
            <option>December</option>
          </select></td>
        <td width="33%" bgcolor="#CCCCCC"><select name="end_year" size="1" class="BodySansSmall">
            <option>2000</option>
            <option>2001</option>
          </select></td>
      </tr>
      <tr>
        <td width="67%" colspan="2" bgcolor="#CCCCCC"><select name="standard" size="1" class="BodySansSmall">
            <option value="Deluxe" selected>Deluxe (over 5,000 Baht)</option>
            <option value="Superior">First-class (up to 5,000 Baht)</option>
            <option value="Standard">Superior (up to 3,000 Baht)</option>
            <option value="Budget">Standard (up to 1,500 Baht)</option>
          </select></td>
        <td width="33%" bgcolor="#CCCCCC">
          <p align="center"><select name="numrooms" size="1" class="BodySansSmall">
            <option selected>1</option>
            <option>2</option>
          </select></td>
      </tr>
      <tr>
        <td width="34%" bgcolor="#CCCCCC"><select size="1" name="location">
            <option>Bangkok</option>
            <option value="samui">Samui</option>
            <option>Phuket</option>
          </select></td>
        <td width="33%" bgcolor="#CCCCCC"><select size="1" name="acc">
            <option>resort</option>
            <option>cottage</option>
          </select></td>
        <td width="33%" bgcolor="#CCCCCC">
          &nbsp;</td>
      </tr>
      <tr>
        <td width="100%" colspan="3" bgcolor="#CCCCCC">
          <p align="center"><input type="submit" value="Submit" name="B1"></td>
      </tr>
    </table>
    </center>
  </div>
  <p>&nbsp;</p>
</form>

</body>

</html>
dastardAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ron WarshawskyConnect With a Mentor Commented:
dastard,

 Can you please, post a sample of the data you are selecting from.

  You can e-mail it to rwarsh@webcombo.net or post it here.

rwarsh
0
 
Ron WarshawskyCommented:
Your query will look like:

rooms - table, that contains rooms information

room_type is your room type

standard, arrdate, end_date, arrmonth, end_month , arryear and end_year - your host variables


select *
from rooms
where
      room_type = :standard
  and not (
        arrdate   >= :arrdate
    and end_date  <= :end_date
    and arrmonth  >= :arrmonth
    and end_month <= :end_month
    and arryear   >= :arryear
    and end_year  <= :end_year
          )  

Regards,
  rwarsh

P.S. Let me know, if you have any questions
0
 
dastardAuthor Commented:
This is interesting but it will not work in the format of my table.
In my table I have the following fields.

Hotel ID
Room type
date 1 to 31 ( so each date is a separate field )
month
year

The record inside the date field 1- 31 is the number of rooms available for this hotel ID
When the search is made, I would like to query so that if the arrival date and end date month year etc  have value greater than 0 then  this should give the final result of ALL matching records and present the Hotel(s) ID.

Providing this query produces the Hotel ID then the next stage where I use PHP3 and MySql to query this ID from another database.

Perhaps you can suggest another way of constructing the database if you feel that the way I am doing this is incorrect or inefficient.

0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Ron WarshawskyCommented:
This table structure will make it normalized:

Hotel ID
Room_Type
Rooms_Available
Date
Month
Year

Then query will look like:

select HotelID
from  Rooms
where Room_Type = :standard
  and Rooms_Available > 0
  and date is between :arrdate and :end_date
  and month is between :arrmonth and :end_month
  and year is between :arryear and :end_year

As always, let me know. if you have any questions.

rwarsh

0
 
dastardAuthor Commented:
When I do this I get multiple records with the same hotel ID. This is because the records have the same hotel ID for the different days.

To try to further clarify the problem,

I need to select Hotel Name, Description, URL from *another* table of hotel information (hotelinfo).
Where the rooms available are selected from another table ( rooms table)  where the rooms available >0 and date is between selected arrday, arrmonth,arryear.

where hotel ID of hotelinformation table = hotel ID for rooms table,
Hope this makes sense.
0
 
csabayCommented:
I don't see among your selects, datard, which one is referring to column Date. Please, explain!

Regards

Charles
0
 
Ron WarshawskyCommented:
dastard,

 I am sorry, please change your query as :


select distinct HotelID
from  Rooms
where Room_Type = :standard
  and Rooms_Available > 0
  and date is between :arrdate and :end_date
  and month is between :arrmonth and :end_month
  and year is between :arryear and :end_year

rwarsh



0
 
Ron WarshawskyCommented:
To select hotel info use this query:

select distinct
           a.HotelID,
           b.Hotel Name,
           b.Description,
           b.URL
from  Rooms a,
      HotelInfo b
where a.HotelID = b.HotelID
  and a.Room_Type = :standard
  and a.Rooms_Available > 0
  and a.date is between :arrdate and :end_date
  and a.month is between :arrmonth and :end_month
  and a.year is between :arryear and :end_year

rwarsh

0
 
dastardAuthor Commented:
Thx Rwarsh but I still cannot get this to work,
I have never seen the command 'distinct' is this a Sql command or not?
I have tried to use as you suggest but no records are found.
Since I am using Php3 I also have tried inserting the $ as follows.


$sql = "select distinct hotelid
from  rooms
where av > 0
  and date  between '$arrdate' and '$end_date'
  and month  between '$arrmonth' and '$end_month'
  and year between '$arryear' and '$end_year'

 ";
As I indicated before, I DO want to select the Hotel ID but from the Hotels where the condition matches for rooms available date etc. Then to go on to the Hotel Description table
eg syntax below

<html>
<?

mysql_connect(   "localhost",   "username",   "password");

mysql_select_db(   "db")  or  die(   "Error  opening database");



    // query the DB

    $sql = "select hotelinfo.name , hotelinfo.url, hotelifo.description from hotelinfo ,rooms
where hotelinfo.hotelid = rooms.hotelid and rooms.av > 0
  and rooms.date  between '$arrdate' and '$end_date'
  and rooms.month  between '$arrmonth' and '$end_month'
  and rooms.year between '$arryear' and '$end_year'

 ";
           

    $result = mysql_query($sql);      

   
   
    if ($myrow = mysql_fetch_array($result))

      // display list if there are records to display

      do

        printf("<a href= %s >%s</a>%s<br>\n", $myrow["url"], $myrow["name"], $myrow["description"]);

       while ($myrow = mysql_fetch_array($result));

     else

      // no records to display

      echo "Sorry, no records were found!";      






?>




0
 
dastardAuthor Commented:
Comment to csbay

I do not want to select the date but I want the date as the condition to select the right record.
0
 
sgantaCommented:
Hi Dastard,

I just want to give a try.

Does the SQL that your working on supports TO_DATE etc. conversion
functions, then you can do the following.

Concatenate the DATE-MONTH-YEAR together and compare with
date.

For eg: In ORACLE we use || for concatenation.

Hotel ID
                 Room_Type
                 Rooms_Available
                 Date
                 Month
                 Year

Suppose :from_date, :to_date, :from_month, :to_month,
:from_year and :two_year are the parameters that you are passing

You can compare it like this

SELECT * FROM hotel
WHERE
         TO_DATE(date||'-'||month||'-'||year,'dd-mon-yyyy')
         between TO_DATE(:from_date||'-'||from_month||'-'||from_year,'dd-mon-yyyy')TO_DAand TO_DATE(:from_date||'-'||from_month||'-'||from_year,'dd-mon-yyyy')

OR

SELECT * FROM hotel
WHERE
         TO_DATE(date||'-'||month||'-'||year,'dd-mon-yyyy')
         between TO_DATE('12-jan-1990','dd-mon-yyyy') and
         between TO_DATE('12-oct-1999','dd-mon-yyyy');

Hope this may help you to convert into your SQL.
0
 
dastardAuthor Commented:
finally got it sorted with a slight modification pasted below
very good thx for your help.
Sorry for the delay posting this

select DISTINCT hotel_name , description from rmav where location= '$location' AND date >= '$arrday' AND date = '$end_day' AND month >='$arrmonth'
AND month = '$end_month' AND year >= '$arryear' AND year ='$end_year'AND av > 0 AND accomodation = '$acc'";
0
 
Ron WarshawskyCommented:
You are very welcome.

   rwarsh
0
 
bernie123Commented:
Im currently working on a similar database but without the leave/end date field, my fields are:

arrival date
Number of nights
room type

Any ideas how i could search for availablity using these fields?  Platform: Oracle 8i.

Cheers Bernie
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.