Jump to content

query in query


craigalways

Recommended Posts

I have 3 queries need help combining the last one. its for my hotel booking system.

so far I have this is for the date ranges
SELECT roomno FROM booking WHERE 2007 -01 -01 <= enddate AND startdate >=2007 -01 -01

and then this to check to see if the room is already booked.
SELECT DISTINCT roomno FROM room WHERE NOT EXISTS ( SELECT * FROM booking WHERE booking.roomno = room.roomno )

I am trying to output a list of room numbers with roomno as the heading so my while loop can pick it up in php.

I would also like to be able to input start date, end date and room type variables.


Here is a list of tables and fields.

Booking

  bookingno               
  customerno               
  roomno               
  startdate     
  enddate

room

  roomno           
  roomtypeID             
  bath               
  shower             
  teacoffee         
  other

roomtype

  roomtypeID         
  roomtype               
  peakrate             
  offpeakrate


Any help at all would be appreciated I have spent days working on it now.
Thanks Craig.




Link to comment
Share on other sites

[quote]
I am trying to output a list of room numbers with roomno as the heading so my while loop can pick it up in php.
[/quote]

[code]
select roomno from room;
// do your php work here to display the results
[/code]

it is that simple, unless you wanted something else.
Link to comment
Share on other sites

Here is how I would do it, assuming I follow you:

First add a field to room called booked.  This will be updated.  if it is booked then it will have a 1, if it isn't booked it will have a 0.  Then do

SELECT * FROM room WHERE booked = 0

That way you don't even mess with the Booking table unless you want to see the booked rooms (this way you can see the free rooms)

Okay, then it gets tricky.  Do a loop and inside this loop have a mysql statement that does:

SELECT * FROM roomtype WHERE roomtypeID = $room['roomtypeID']

So it might look something like this:

[code]

$query = "SELECT * FROM room WHERE booked=0";
$execute = mysql_query($query);

while($room=mysql_fetch_array($execute)) {

$q = "SELECT * FROM roomtype WHERE roomtypeID = $room['roomtypeID']";
$row = mysql_fetch_row($result);

echo $row[0]; // zero because it will only return one result per loop

}
[/code]

Hope that helps.  I haven't tested it, but it should work.
Link to comment
Share on other sites

You do not have to create any records at all.  You posted the 3 table structures, and now we need to know what you wanted out of those 3 tables.  like, "I like to retrieve all available rooms from date 1 to date 2.  Something like this would make more sense.  Know what I meant?
Link to comment
Share on other sites

You can achieve this query with both sub-query and right join

These are your search criteria variables:
$my_roomtype :  The specific numerical roomtype
$my_startdate : The start date
$my_enddate :  The end date

first we need to select all rooms that are booked during that my_startdate and my_enddate an:

"select roomno from booking where (startdate between '$my_startdate' and '$my_enddate') or (enddate between '$my_startdate' and '$my_enddate') or (startdate > '$my_startdate' and enddate < '$my_enddate')";

now, we will select all rooms that not belong to any of the room above:
"select roomno from room where roomno NOT IN (select roomno from booking where (startdate between '$my_startdate' and '$my_enddate') or (enddate between '$my_startdate' and '$my_enddate') or (startdate > '$my_startdate' and enddate < '$my_enddate'))";

the query above is not what you wanted because it does not filter out roomtype, so we need a join:

Final query:
"select roomno from room where roomno NOT IN (select roomno from booking where (startdate between '$my_startdate' and '$my_enddate') or (enddate between '$my_startdate' and '$my_enddate') or (startdate > '$my_startdate' and enddate < '$my_enddate')) right join roomtype.roomtype on (roomtype.roomtype = $my_roomtype);

Note: for this to work, you need to use appropriate datatype for start and end date. Use date, datetime or unix time stamp would be good.  Date is the best cuz you really don't need the hour/min in this case.  If you use plain text as your date, it won't work.
roomtype must some kind of number format too.

If you use this query and it won't work, post the error here, because I just type it on top of my head without testing.

Good Luck

Link to comment
Share on other sites

It work fine up until the right join
[code]
SQL query: 

SELECT roomno
FROM room
WHERE roomno NOT
IN (


SELECT roomno
FROM booking
WHERE (
startdate
BETWEEN '2007-01-01'
AND '2007-01-01'
)
OR (
enddate
BETWEEN '2007-01-01'
AND '2007-01-01'
)
OR (
startdate > '2007-01-01'
AND enddate < '2007-01-01'
)
)
RIGHT JOIN roomtype.roomtype ON ( roomtype.roomtype = '1' )
LIMIT 0 , 30

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'right join roomtype . roomtype on ( roomtype . roomtype = '1' ) 
LIMIT 0, 30' at line 1
[/code]

The first part works fine thanks.


Link to comment
Share on other sites

[code]
SELECT roomno
FROM room
RIGHT JOIN roomtypeID.room ON ( roomtypeID.roomtype = '1' )
WHERE roomno NOT
IN (


SELECT roomno
FROM booking
WHERE (
startdate
BETWEEN '2007-01-01'
AND '2007-01-01'
)
OR (
enddate
BETWEEN '2007-01-01'
AND '2007-01-01'
)
OR (
startdate > '2007-01-01'
AND enddate < '2007-01-01'
)
)
LIMIT 0 , 30

MySQL said: 

#1142 - SELECT command denied to user 'craig'@'localhost' for table 'room' [/code]
still not sure about the names of the tables and to fields on the join. Any ideas?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.