sqlÖÐjoin onµÄÓ÷¨
join on ÓÃÓÚÔÚ¶à¸ö±íÖ®¼ä°´Ö¸¶¨ÁÐÆ¥ÅäÐУ¬·µ»ØÅþÁ¬Ð§¹û¡£°ì·¨°üÀ¨£ºÖ¸¶¨Ä¿µÄ±í¡¢ÅþÁ¬ÀàÐÍ£¨inner¡¢left¡¢right¡¢full£©ºÍÅþÁ¬Ìõ¼þ£¨on ×Ó¾äÖÐÁ½¸ö±íµÄÆ¥ÅäÁУ©¡£
SQL ÖÐ JOIN ON µÄÓ÷¨
JOIN ON µÄ×÷ÓÃ
JOIN ON ÓÃÓÚÔÚ¶à¸ö±íÖ®¼ä½¨ÉèÅþÁ¬£¬Æ¥ÅäÌض¨ÁеÄÖµ£¬²¢½«ÅþÁ¬Ð§¹û×÷ΪÐÂ±í·µ»Ø¡£
Óï·¨½á¹¹
SELECT ÁÐ1, ÁÐ2, ... FROM ±í1 JOIN ±í2 ON ±í1.ÁÐÃû = ±í2.ÁÐÃû;
µÇ¼ºó¸´ÖÆ
°ì·¨
Ö¸¶¨Ä¿µÄ±í£ºFROM ×Ó¾äÖ¸¶¨ÐèÒªÅþÁ¬µÄ±í»òÊÓͼ¡£
Ö¸¶¨ÅþÁ¬ÀàÐÍ£ºJOIN Òªº¦×ÖÖ¸¶¨ÅþÁ¬ÀàÐÍ£¬Èç INNER JOIN¡¢LEFT JOIN µÈ¡£
Ö¸¶¨ÅþÁ¬Ìõ¼þ£ºON ×Ó¾äÖ¸¶¨ÅþÁ¬µÄÌõ¼þ£¬¼´Á½¸ö±íÖÐҪƥÅäµÄÁС£
ʾÀý
ÒÔÏÂÅÌÎÊʹÓà JOIN ON ´Ó Customers ºÍ Orders ±íÖзµ»Ø¿Í»§ÐÅÏ¢ºÍ¶©µ¥ÐÅÏ¢£º
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderDate FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
µÇ¼ºó¸´ÖÆ
ÅþÁ¬ÀàÐÍ
SQL Ö§³Ö¶àÖÖÅþÁ¬ÀàÐÍ£¬°üÀ¨£º
INNER JOIN£º½ö·µ»ØÁ½¸ö±íÖÐÆ¥ÅäµÄÐС£
LEFT JOIN£º·µ»Ø×ó±íÖеÄËùÓÐÐУ¬ÒÔ¼°ÓëÓÒ±íÆ¥ÅäµÄÐУ¨ÈôÊÇûÓÐÆ¥Å䣬Ôò·µ»Ø NULL£©¡£
RIGHT JOIN£º·µ»ØÓÒ±íÖеÄËùÓÐÐУ¬ÒÔ¼°Óë×ó±íÆ¥ÅäµÄÐУ¨ÈôÊÇûÓÐÆ¥Å䣬Ôò·µ»Ø NULL£©¡£
FULL OUTER JOIN£º·µ»ØÁ½¸ö±íÖеÄËùÓÐÐУ¬ÎÞÂÛÊÇ·ñÆ¥Åä¡£
×î¼Ñʵ¼ù
ʹÓÃË÷ÒýÓÅ»¯ÅþÁ¬ÐÔÄÜ¡£
Ã÷È·Ö¸¶¨ÅþÁ¬ÁУ¬×èÖ¹ÆçÒå¡£
ƾ֤ӪҵÂ߼ѡÔñºÏÊʵÄÅþÁ¬ÀàÐÍ¡£
ÒÔÉϾÍÊÇsqlÖÐjoin onµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡