mysqlÖÐonµÄÓ÷¨
on ×Ó¾äÓÃÓÚÖ¸¶¨ join ²Ù×÷ÖнÏÁ¿ºÍºÏ²¢Á½¸ö±íµÄÐеÄÌõ¼þ£¬°üÀ¨ÄÚÅþÁ¬¡¢×óÍâÅþÁ¬¡¢ÓÒÍâÅþÁ¬ºÍÈ«ÍâÅþÁ¬¡£on Ìõ¼þֻʹÓÃÏàµÈ½ÏÁ¿£¬¿ÉÒÔʹÓöà¸öÌõ¼þÖ¸¶¨¸üÖØ´óµÄÅþÁ¬¹æÔò£¬²¢¿ÉÓëÆäËû join ÀàÐÍÒ»ÆðʹÓá£
MySQL ÖÐ ON µÄÓ÷¨
ON ×Ó¾ä¸ÅÊö
ON ×Ó¾äÓÃÓÚÖ¸¶¨ÔÚ JOIN ²Ù×÷ÖнÏÁ¿ºÍºÏ²¢Á½¸ö±íµÄÐеÄÌõ¼þ¡£
Óï·¨
ON ±í1.ÁÐÃû = ±í2.ÁÐÃû
µÇ¼ºó¸´ÖÆ
Ó÷¨
ÄÚÅþÁ¬ (INNER JOIN)£ºÖ»·µ»ØÁ½¸ö±íÖÐÆ¥ÅäµÄÐС£
×óÍâÅþÁ¬ (LEFT OUTER JOIN)£º·µ»Ø×ó±íÖеÄËùÓÐÐУ¬ÒÔ¼°ÈκÎÔÚÓÒ±íÖÐÆ¥ÅäµÄÐС£ÈôÊÇÓÒ±íÖÐûÓÐÆ¥Å䣬Ôò·µ»Ø NULL¡£
ÓÒÍâÅþÁ¬ (RIGHT OUTER JOIN)£ºÓë×óÍâÅþÁ¬ÀàËÆ£¬µ«·µ»ØÓÒ±íÖеÄËùÓÐÐС£
È«ÍâÅþÁ¬ (FULL OUTER JOIN)£º·µ»ØÁ½¸ö±íÖеÄËùÓÐÐУ¬ÎÞÂÛÊÇ·ñÆ¥Åä¡£
ʾÀý
ÄÚÅþÁ¬£º
SELECT * FROM ±í1 INNER JOIN ±í2 ON ±í1.id = ±í2.id;
µÇ¼ºó¸´ÖÆ
×óÍâÅþÁ¬£º
SELECT * FROM ±í1 LEFT OUTER JOIN ±í2 ON ±í1.id = ±í2.id;
µÇ¼ºó¸´ÖÆ
ÓÒÍâÅþÁ¬£º
SELECT * FROM ±í1 RIGHT OUTER JOIN ±í2 ON ±í1.id = ±í2.id;
µÇ¼ºó¸´ÖÆ
È«ÍâÅþÁ¬£º
SELECT * FROM ±í1 FULL OUTER JOIN ±í2 ON ±í1.id = ±í2.id;
µÇ¼ºó¸´ÖÆ
×¢ÖØÊÂÏî
ON Ìõ¼þÖ»ÄÜʹÓÃÏàµÈ½ÏÁ¿£¨=£©¡£
¿ÉÒÔʹÓöà¸ö ON Ìõ¼þÀ´Ö¸¶¨¸üÖØ´óµÄÅþÁ¬¹æÔò¡£
ON ×Ó¾ä¿ÉÒÔÓëÆäËû JOIN ÀàÐÍ£¨Èç CROSS JOIN¡¢NATURAL JOIN£©Ò»ÆðʹÓá£
ÒÔÉϾÍÊÇmysqlÖÐonµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡