Solved

Sql date search problem

Posted on 2000-02-20
14
3,167 Views
Last Modified: 2008-01-09
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>
0
Comment
Question by:dastard
14 Comments
 
LVL 3

Expert Comment

by:rwarsh
ID: 2542998
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
 

Author Comment

by:dastard
ID: 2548743
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
 
LVL 3

Expert Comment

by:rwarsh
ID: 2548934
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
 

Author Comment

by:dastard
ID: 2549595
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
 

Expert Comment

by:csabay
ID: 2550126
I don't see among your selects, datard, which one is referring to column Date. Please, explain!

Regards

Charles
0
 
LVL 3

Expert Comment

by:rwarsh
ID: 2550452
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
 
LVL 3

Expert Comment

by:rwarsh
ID: 2550476
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dastard
ID: 2553058
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
 

Author Comment

by:dastard
ID: 2553063
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
 
LVL 4

Expert Comment

by:sganta
ID: 2553642
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
 
LVL 3

Accepted Solution

by:
rwarsh earned 500 total points
ID: 2554525
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
 

Author Comment

by:dastard
ID: 2571365
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
 
LVL 3

Expert Comment

by:rwarsh
ID: 2571404
You are very welcome.

   rwarsh
0
 

Expert Comment

by:bernie123
ID: 7519452
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PDO and Database 1 88
sql query help 7 83
sql calculate reminders 11 56
Slow SQL query 12 27
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now