Uploaded by Lam Nguyen Xuan

JOIN (1)

advertisement
JOIN
mySQL
join.pptx
Trang 1 / 9
join.pptx
Trang 2 / 9
join.pptx
Trang 3 / 9
The relationship between the two tables is specified by the
customer_id key . It is the "primary key" in customer table and
the "foreign key" in order table.
CI firstname lastname email
1
George
address
city
state
ZC
Washing gwashington 3200 Mt Vernon
ton
@usa.gov
Hwy
Mount
Vernon
VA
22121
Quincy
MA
Charlottesv VA
ille
2
John
Adams
jadams@usa 1250 Hancock St
.gov
3
Thomas
Jefferso tjefferson@u 931 Thomas
n
sa.gov
Jefferson Pkwy
4
James
Madison jmadison@us 11350 Constitution Orange
a.gov
Hwy
5
James
Monroe
jmonroe@us 2050 James
a.gov
Monroe Parkway
OrI order_date
amount CI
1
07/04/1776
$234.56 1
02169
2
03/14/1760
$78.50
22902
3
05/23/1784
$124.00 2
4
09/03/1790
$65.50
3
5
07/21/1795
$25.50
10
6
11/27/1787
$14.40
9
VA
22960
Charlottesv VA
ille
22902
join.pptx
Trang 4 / 9
3
The relationship between the two tables is specified by the
customer_id key . It is the "primary key" in customer table and
the "foreign key" in order table.
CI firstname lastname email
1
George
address
city
state
ZC
Washingt gwashingto 3200 Mt Vernon
on
n@usa.gov Hwy
Mount
Vernon
VA
22121
Quincy
MA
Charlottesv VA
ille
2
John
Adams
jadams@us 1250 Hancock St
a.gov
3
Thomas
Jefferson tjefferson@ 931 Thomas
usa.gov
Jefferson Pkwy
4
James
Madison
jmadison@
usa.gov
11350 Constitution Orange
Hwy
5
James
Monroe
jmonroe@u 2050 James
sa.gov
Monroe Parkway
OrI order_date
amount CI
1
07/04/1776
$234.56 1
02169
2
03/14/1760
$78.50
22902
3
05/23/1784
$124.00 2
4
09/03/1790
$65.50
3
5
07/21/1795
$25.50
10
6
11/27/1787
$14.40
9
VA
22960
Charlottesv VA
ille
22902
join.pptx
Trang 5 / 9
3
Inner Join
SELECT firstname, lastname, amount
FROM customer c
INNER JOIN order o
ON c.CI= o.CI
first_name
last_name
order_amount
George
Washington
$234.56
John
Adams
$124.00
Thomas
Jefferson
$78.50
Thomas
Jefferson
$65.50
join.pptx
Trang 6 / 9
Left join
SELECT firstname, lastname, amount
FROM customer c
LEFT JOIN order o
ON c.CI= o.CI
first_name
last_name
order_amount
George
Washington
$234.56
John
Adams
$124.00
Thomas
Jefferson
$78.50
Thomas
Jefferson
$65.50
James
Madison
NULL
James
Monroe
NULL
join.pptx
Trang 7 / 9
Right join
SELECT firstname, lastname, amount
FROM customer c
RIGHT JOIN order o
ON c.CI= o.CI
first_name
last_name
order_amount
George
Washington
$234.56
Thomas
Jefferson
$78.50
John
Adams
$124.00
Thomas
Jefferson
$65.50
NULL
NULL
$25.50
NULL
NULL
$14.40
Right joining the orders table to the customers table
join.pptx
Trang 8 / 9
Left vs Right join
• The order in which the tables are joined is important.
• Right join the customer table to the order table?
 The result would be the same as left joining the order table to the
customer table
join.pptx
Trang 8 / 9
Full join
SELECT firstname, lastname, amount
FROM customer c
FULL JOIN order o
ON c.CI= o.CI
first_name
last_name
order_amount
George
Washington
$234.56
Thomas
Jefferson
$78.50
John
Adams
$124.00
Thomas
Jefferson
$65.50
NULL
NULL
$25.50
NULL
NULL
$14.40
James
Madison
NULL
James
Monroe
NULL
join.pptx
Trang 9 / 9
Download