Jump to content

help with joins


zang8027

Recommended Posts

I got 2 tables:

 

table "hotels"

 

hotel_id  | hotel_name

 

 

 

table "hotelsToCity"

 

hotel_id | city_id

 

 

 

 

 

Now, im using the following query to gain the hotel id from hotelsToCity:

 

$quer3=mysql_query("SELECT DISTINCT hotel_id, city_id FROM hotelsToCity where city_id=$subcat ORDER BY city_id");

 

How can i use a join (inner or left.. idont know which is best to use) in that query.

 

 

 

 

 

Link to comment
Share on other sites

SELECT DISTINCT htc.hotel_id, h.hotel_name, htc.city_id FROM hotelsToCity htc JOIN hotels h ON h.hotel_id = htc.hotel_id;

 

 

 

You would want a LEFT JOIN if you want to pull records from hotelsToCity that do not have entries in the hotel table, but that shouldn't ever happen.

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.