Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use INNER JOIN statement using a bridge table

Basically I have a table "Products" with "Product_ID" and table "Sub_Categories" with "Sub_Category_ID".

Products that have a "Sub_Category" are linked using a bridge table, "SubC_Prod_Bridge", they are linked by ID "Products" and "Sub_Category_ID".

Like this:

Table: Products
Product_ID
4

Table: Sub_Categories
Sub_Category_ID
5

Table: SubC_Prod_Bridge
Product_ID   Sub_Category_ID
4            5      

I know this question is very basic. What I am really looking for is a good reference online for JOIN statements, any recommendations are really appreciated.

like image 512
TaylorMac Avatar asked Jan 09 '12 00:01

TaylorMac


People also ask

Which method do we use for joining two tables?

A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

How is the inner join like the where clause when joining tables?

INNER JOIN is ANSI syntax whereas the WHERE syntax is more relational model oriented. The INNER JOIN is generally considered more readable and it is a cartesian product of the tables, especially when you join lots of tables but the result of two tables JOIN'ed can be filtered on matching columns using the WHERE clause.

What is an inner join table?

An inner join combines records from two tables based on a join predicate and requires that each record in the first table has a matching record in the second table. Thus, inner joins return only records from both joined tables that satisfy the join condition.


1 Answers

SELECT P.*, S.*
FROM Products P
JOIN SubC_Prod_Bridge B
  ON P.Product_ID = B.Product_ID
JOIN Sub_Categories S
  ON S.Sub_Category_ID = B.Sub_Category_ID
like image 80
Amadan Avatar answered Sep 27 '22 22:09

Amadan