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

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

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?

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.

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.

The alias/table designation is not a part of the name in the result.

Do a var_dump () on the $row variable, to see what it contains.

 

Also, you'll need to turn on error reporting. Ref. Jessica's signature.

Received error:

 

Column 'hotelname' in field list is ambiguous

 

So i tried:

 

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

 

That was the correct thing to do, but the results will still be $row['hotelname'], $row['room'] etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.