Lesson3-MYSQL JOIN-2
Lesson3-MYSQL JOIN-2
SELECT
select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;
In this syntax:
• First, specify the main table that appears in
the FROM clause (t1).
• Second, specify the table that will be joined
with the main table, which appears in the
INNER JOIN clause (t2, t3,…).
• Third, specify a join condition after the ON
keyword of the INNER JOIN clause. The
join condition specifies the rule for matching
rows between the main table and the table
that appeared in the INNER JOIN clause.
• Assuming that you want to join two tables
t1 and t2.
SELECT
select_list
FROM
t1
INNER JOIN t2 ON join_condition;
• The INNER JOIN clause compares each
row in the t1 table with every row in the t2
table based on the join condition.
SELECT orderNumber,productName,
msrp,priceEach
FROM products p
INNER JOIN orderdetails o
ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE p.productcode = 'S10_1678';
MySQL LEFT JOIN
• The LEFT JOIN allows you to query data
from two or more tables. Similar to the
INNER JOIN clause, the LEFT JOIN is an
optional clause of the SELECT statement,
which appears immediately after the FROM
clause.
• Suppose that you want to join two tables t1
and t2.
• The following statement shows how to use
the LEFT JOIN clause to join the two tables:
• The following statement shows how to use
the LEFT JOIN clause to join the two tables:
SELECT
select_list
FROM
t1
LEFT JOIN t2 ON
join_condition;
• When you use the LEFT JOIN clause, the
concepts of the left table and the right table
are introduced.