Jump to content

Question about inner join


eldan88

Recommended Posts

Hey Guys. I am learning about inner join, and I am a little confused about how the syntax work

 

On one example there is the following syntax

 

$query = "SELECT family.Position, food.Meal ".
"FROM family, food ".
    "WHERE family.Position = food.Position";

 

On all the other examples I came across the following syntax

 

SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS
.ID = ORDERS.CUSTOMER_ID;

 

 

 

Why on the first example the table name is prepend to the column name on the select statement? and why there is no syntax "INNER JOIN ON"

 

Thanks for your help in advance!

Link to comment
Share on other sites

Use the second syntax (ie INNER JOIN .. ON).

 

1. The first syntax can only be used for the (default) INNER JOIN. If you want a LEFT or RIGHT JOIN you need the second syntax so keep it consistent.

2. The second syntax separates the structure of the query from the selection criteria in the WHERE clause.

3. Your queries will run faster.

 

Strictly you only need to specify table.column where two or  more tables contain the same column name thus removing ambiguity about which one to use. Personally I prefer to document the origin of each column using a short table alias (especially if the table name is something like "bacterialogicalculturegrowthstatistitics") so I would write that second query as

SELECT c.ID, c.NAME, o.AMOUNT, o.DATE
FROM CUSTOMERS c
     INNER JOIN ORDERS o 
        ON c.ID = o.CUSTOMER_ID;
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.