COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
ON <JOIN CONDITION>
WHERE <CRITERIANS>
ORDER BY <COLUMN LIST>
[ASC / DESC]
Ví duï 8-23: INNER JOIN vôùi moät soá coät chæ ñònh
/* in ra danh saùch khaùch haøng mua haøng trong thaùng 10 */
Select CustName,OrderID,
OrderDate,Amount,
TotalAmount
from tblCustomers
inner join tblOrders
on tblCustomers.CustID = tblOrders.CustID
where month (OrderDate) = 10
order by CustName
Keát quaû traû veà nhö sau:
CustName
OrderID OrderDate .. TotalAmount
---------------------------------------------
CENTURY Hotel 13 2001-10-17 388800000
CENTURY Hotel 14 2001-10-18 518400000
CENTURY Hotel 16 2001-10-17 388800000
CENTURY Hotel 17 2001-10-18 14400000
CENTURY Hotel 18 2001-10-18 12960000
CENTURY Hotel 110 2001-10-18 216000000
Plaza Hotel 12 2001-10-17 403200000
Plaza Hotel 19 2001-10-17 86400000
Plaza Hotel 11 2001-10-17 576000000
Plaza Hotel 15 2001-10-17 288000000
Neáu baïn caàn laáy ra taát caû caùc coät trong caùc baûng coù keát noái laïi vôùi nhau baèng meänh ñeà INNER
JOIN, cuù phaùp treân coù theå vieát laïi nhö sau:
SELECT first_tablename.*,
second_tablename.*
[,next table name]
FROM <first_tablename>
INNER JOIN <second_tablename>
ON <join conditions>
[INNER JOIN <next_tablename>
ON <join conditions>]
WHERE <conditions>
ORDER BY <column list>
[ASC / DESC]
Ví duï 8-24: INNER JOIN vôùi taát caùc tröôøng lieân quan
/* in ra danh saùch khaùch haøng mua haøng trong thaùng 10 */
Select CustID,CustName,OrderID,
OrderDate,TotalAmount
from tblCustomers
inner join tblOrders
On TblCustomers.CustID=tblOrders.CustID
where month (OrderDate) = 10
order by CustName DESC
Keát quaû traû veà nhö sau:
Giaùo vieân: Phaïm Höõu Khang