sqlÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇʲô
sql ÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇÁ½ÖÖÅþÁ¬±íµÄ·½·¨£º×óÅþÁ¬£º±£´æ×ó±íËùÓÐÐУ¬×ÝÈ»ÓÒ±íûÓÐÆ¥ÅäÐУ¬ÓÒ±í×Ö¶ÎΪ¿Õ¡£ÓÒÅþÁ¬£º±£´æÓÒ±íËùÓÐÐУ¬×ÝÈ»×ó±íûÓÐÆ¥ÅäÐУ¬×ó±í×Ö¶ÎΪ¿Õ¡£
SQL ÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬
ÔÚ SQL ÖУ¬ÅþÁ¬ÊÇδÀ´×ÔÁ½¸ö»ò¸ü¶à±íµÄÐÐ×éºÏÔÚÒ»ÆðµÄÀú³Ì¡£×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇÅþÁ¬±íµÄÁ½ÖÖÌض¨ÀàÐÍ¡£
×óÅþÁ¬
±£´æ×ó±íÖеÄËùÓÐÐУ¬×ÝÈ»ÔÚÓÒ±íÖÐûÓÐÆ¥ÅäµÄÐС£
¹ØÓÚÓÒ±íÖÐûÓÐÆ¥ÅäÐеÄ×ó±íÐУ¬ÓÒ±íÖеÄ×ֶν«ÏÔʾΪ NULL¡£
ÓÒÅþÁ¬
±£´æÓÒ±íÖеÄËùÓÐÐУ¬×ÝÈ»ÔÚ×ó±íÖÐûÓÐÆ¥ÅäµÄÐС£
¹ØÓÚ×ó±íÖÐûÓÐÆ¥ÅäÐеÄÓÒ±íÐУ¬×ó±íÖеÄ×ֶν«ÏÔʾΪ NULL¡£
Óï·¨
×óÅþÁ¬£º
SELECT * FROM ×ó±í LEFT JOIN ÓÒ±í ON ×ó±íÀ¸ = ÓÒ±íÀ¸;
µÇ¼ºó¸´ÖÆ
ÓÒÅþÁ¬£º
SELECT * FROM ÓÒ±í RIGHT JOIN ×ó±í ON ×ó±íÀ¸ = ÓÒ±íÀ¸;
µÇ¼ºó¸´ÖÆ
ʾÀý
˼Á¿ÒÔÏÂÁ½¸ö±í£º
×ó±í£º +----+--------+ | id | Ãû³Æ | +----+--------+ | 1 | John | | 2 | Mary | +----+--------+ ÓÒ±í£º +----+---------+ | id | µØµã | +----+---------+ | 1 | Main St | | 3 | Oak Ave | +----+---------+
µÇ¼ºó¸´ÖÆ
×óÅþÁ¬£º
SELECT * FROM ×ó±í LEFT JOIN ÓÒ±í ON ×ó±í.id = ÓÒ±í.id;
µÇ¼ºó¸´ÖÆ
Êä³ö£º
+----+--------+---------+ | id | Ãû³Æ | µØµã | +----+--------+---------+ | 1 | John | Main St | | 2 | Mary | NULL | +----+--------+---------+
µÇ¼ºó¸´ÖÆ
ÓÒÅþÁ¬£º
SELECT * FROM ÓÒ±í RIGHT JOIN ×ó±í ON ×ó±í.id = ÓÒ±í.id;
µÇ¼ºó¸´ÖÆ
Êä³ö£º
+----+---------+--------+ | id | µØµã | Ãû³Æ | +----+---------+--------+ | 1 | Main St | John | | 3 | Oak Ave | NULL | +----+---------+--------+
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡