Jump to content

Some help in INNER JOIN COMMAND


genzedu777

Recommended Posts

Hi all,

 

I have some questions regarding INNER JOIN.

 

Currently I have 4 tables...

1) tutor_preferred_district (tutor_preferred_district.jpg)

2) tutor_preferred_dlocation  (tutor_preferred_dlocation.jpg) (Which has tutor_id in it)

 

3) tutor_preferred_zone (tutor_preferred_zone.jpg)

3) tutor_preferred_zone (tutor_preferred_zlocation.jpg) (Which has tutor_id in it)

 

I thought of querying a $query, which consists of all these 4 tables tied to a common ID called 'tutor_id'. How do I achieve it? Below are the codes, I know the codes are wrong, I should have used INNER JOIN, but apparently I do not know how to join it, hence I have used twice of 'FROM' command, hopefully you can understand what I would like to achieve

 

<?php
$query = "SELECT tpd.district_id, tpd.district_name, tpz.zone_id, tpz.zone_name	  
                 FROM tutor_preferred_dlocation AS tpdl 
                 FROM tutor_preferred_zlocation AS tpzl 
                 WHERE tpdl.tutor_id = '{$tutor_id}';"
?>

 

Thank you so much

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi jesirose,

 

Appreciate your reply. I understand how INNER JOIN works, but currently I am trying to join the 4 tables together into ONE query.

 

How do I achieve it? Below are my codes, do you think it's possible in the first place?

 

<?php
$query = "SELECT tpd.district_id, tpd.district_name, tpz.zone_id, tpz.zone_name
       (1)
	 FROM tutor_preferred_district AS tpd
	 INNER JOIN tutor_preferred_dlocation AS tpdl USING (district_id)
	 //How to join (1) & (2) together?
	 (2)
	 INNER JOIN tutor_preferred_zlocation AS tpzl USING (???)
	 INNER JOIN tutor_preferred_zone AS tpz USING (zone_id)

                 WHERE tpdl.tutor_id = '{$tutor_id}';"
?>

Link to comment
Share on other sites

Hi jesirose,

 

Really appreciate if you can be more specific? What do you mean by

You got the first part, now you need "ON"

 

If it's possible, could you elaborate more in the codes? Thanks

 

$query = "SELECT tpd.district_id, tpd.district_name, tpz.zone_id, tpz.zone_name
       (1)
       FROM tutor_preferred_district AS tpd
       INNER JOIN tutor_preferred_dlocation AS tpdl USING (district_id)
       //How to join (1) & (2) together?
       (2)
       INNER JOIN tutor_preferred_zlocation AS tpzl USING (???)
       INNER JOIN tutor_preferred_zone AS tpz USING (zone_id)

                 WHERE tpdl.tutor_id = '{$tutor_id}';"

 

 

Link to comment
Share on other sites

I could be more specific, but if you had looked up the documentation for a join you would see what ON is.

IMO http://www.gotapi.com/ is a great source for docs. Add the MySQL version and look up JOIN. Read it and look for the word ON. then read the examples. Then try it and post your new code. If your new code doesn't work, we can go from there.

Link to comment
Share on other sites

Hi jesirose,

 

I understand where you are coming from, if you look carefully at the 4 pictures or tables...

 

//The primary key for tutor_preferred_dlocation.sql is district_id, and that is the reason why I can link tutor_preferred_dlocation & tutor_preferred_district together, because they shared the same common key

1) tutor_preferred_dlocation  (tutor_preferred_dlocation.jpg)

2) tutor_preferred_district (tutor_preferred_district.jpg)

 

//The primary key for tutor_preferred_zlocation.sql is zone_id, and that is the reason why I can link tutor_preferred_zlocation & tutor_preferred_zone  together, because they shared the same common key

3) tutor_preferred_zlocation (tutor_preferred_zlocation.jpg)

4) tutor_preferred_zone (tutor_preferred_zone.jpg)

 

Now, my first question

1) Can I still link (1)(2) with (3)(4), is it possible?

2) If it's possible, then how? I have read through LEFT JOIN & RIGHT JOIN, even ON. I don't seem to find an answer, the common denominator is still...you need a common primary key to link the tables together.

 

Please correct me if I am wrong as I am still new in php and sql, would appreciate any advice from you. Thanks

 

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.