Solved

How do you construct a INNER JOIN statement from three tables?

Posted on 2011-09-18
6
208 Views
Last Modified: 2012-06-22
Experts,

I am experimenting with this and trying to learn...  

How do you contruct a INNER JOIN statement from three tables?  Please explain in simple terms.

Table1.subcatname  =  Table2.subcategory

Table1.sku = Table3.productid

Thanks for your help!!





0
Comment
Question by:rlb1
  • 3
  • 2
6 Comments
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 200 total points
ID: 36557812
something like this....

 
select * 

from 

Table1 T1
left join Table2 on (T1.subcatname = T2.subcategory)
left join Table3 (T1.sku = T3.productid)

Open in new window


You have two separate join statements where you put the table you are joining to and then after the ON keyword you show the "link" or relationship between the two tables.  you don't need to use the table aliases but it does make for less typing.
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36557817
exactly like this...

SELECT * FROM table1
INNER JOIN table2 ON table1.subcatname = table2.subcategory
INNER JOIN table3 ON table1.sku = table3.productid

The left join will give you (potentially) empty subcategories and products. Inner Join forces the relationship to be there.

Cheers!
0
 
LVL 7

Accepted Solution

by:
NauticalNonsense earned 300 total points
ID: 36557829
Also, if your Subcatname is a string, consider changing that to a primary key ID column (integer)... that way you can change the name of the subcategory, and the original table is unharmed.

SELECT Products.ProductName, SubCategory.Name
FROM Products
INNER JOIN SubCategory ON Products.SubCategoryId = SubCategory.Id

Where your subcategory table is named SubCategory

Or you could take it a step farther and make products fall into multiple categories with a link table.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rlb1
ID: 36587014
I really appreciate your help... I am finishing another project.  Will be back shortly.
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36587301
Rock on... I don't know how I ended up on this to begin with, but my syntax is for MS SQL server specifically, I'm not sure what your db is... but the principles are the same, your syntax may be slightly different.

Please come back and let us know how it went!
0
 

Author Closing Comment

by:rlb1
ID: 36817479
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now