Jump to content

Two Tables - Select item based on information in both tables?


loren646

Recommended Posts

Problem: I want it to only display the vacancy if it's in the location specified. Table "hotel" knows the "location" but table "vacancy" does not (however it does have the same exact hotel name).

 

i have this right now (which only works on table vacancy):

 

"SELECT * FROM vacancy WHERE price > $minprice AND price < $maxprice"

 

---------------------------below are the mysql table structure and user supplied information--------------------------

 

Prospective Hotel Guest supplies:

Location

Max Price/Min Price

Minimum Price

Quality of Hotel

 

table hotel

primary column1 = hotel name

column 2 = hotel owner

column 3 = location of hotel

column 4 = quality of hotel

 

table vacancy

(not primary) column 1 = hotel name

column 2 = room #

column 3 = price

Edited by loren646
Link to comment
Share on other sites

You need to join the tables

 

http://www.phpfreaks...ta-joins-unions

 

SELECT hotelname, room, price
FROM vacancy as v
INNER JOIN hotel as h
ON v.hotelname = h.hotelname
WHERE v.price BETWEEN $minprice AND $maxprice
AND h.location = '$location'

 

The hotel table should be the only place in your database that holds the hotel name. The hotels should have a unique id column and it is that id that should be in the vacancy table to link them

Edited by Barand
Link to comment
Share on other sites

You need to join the tables

 

http://www.phpfreaks...ta-joins-unions

 

SELECT hotelname, room, price
FROM vacancy as v
INNER JOIN hotel as h
ON v.hotelname = h.hotelname
WHERE v.price BETWEEN $minprice AND $maxprice
AND h.location = '$location'

 

The hotel table should be the only place in your database that holds the hotel name. The hotels should have a unique id column and it is that id that should be in the vacancy table to link them

 

Hotel name is the unique column. it never repeats so i set it as primary. And I thought to use that to link both tables. is that right logic?

Edited by loren646
Link to comment
Share on other sites

Hotel name is the unique column. it never repeats so i set it as primary. And I thought to use that to link both tables. is that right logic?

 

sorry i wasn't being clear hotel name is pretty much an Identification for the hotel. Not exactly the name. Similar to a building address.

Link to comment
Share on other sites

Hotel name is the unique column. it never repeats so i set it as primary. And I thought to use that to link both tables. is that right logic?

 

Received error:

 

Column 'hotelname' in field list is ambiguous

 

So i tried:

 

SELECT v.hotelname, v.room, v.price

 

now it has no errors. but it outputs nothing when run. I tried changing the echo to $row['a.hotelname'] ,$row['v.room'] $row['v.price'] instead of hotelname, room, price

 

still nothing outputs.

 

----

 

it works perfectly fine up until adding this second table.

Edited by loren646
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.