mysqlÖÐjionÓ÷¨
mysql ÖÐµÄ join ²Ù×÷ÓÃÓںϲ¢À´×ÔÁ½¸ö»ò¸ü¶à±íÖеÄÊý¾Ý¡£Ëüͨ¹ýÖ¸¶¨Ìõ¼þÅþÁ¬¾ßÓÐÏà¹ØÁÐÖµµÄ±í£¬³£¼ûÀàÐÍ°üÀ¨£ºinner join£º·µ»Ø¾ßÓÐÆ¥ÅäÐеÄÐС£left join£º·µ»Ø×ó±íËùÓÐÐУ¬×ÝÈ»ÔÚÓÒ±íÖÐûÓÐÆ¥Åä¡£right join£º·µ»ØÓÒ±íËùÓÐÐУ¬×ÝÈ»ÔÚ×ó±íÖÐûÓÐÆ¥Åä¡£full join£º·µ»ØÁ½¸ö±íÖеÄËùÓÐÐУ¬×ÝȻûÓÐÆ¥Åä¡£join µÄÀûÒæ°üÀ¨ºÏ²¢Êý¾Ý¡¢ïÔÌÈßÓàºÍÌá¸ßÅÌÎÊÐÔÄÜ¡£
MySQL ÖÐ JOIN Ó÷¨
JOIN ÊÇ MySQL ÖÐÅþÁ¬Á½¸ö»ò¸ü¶à±íµÄÊý¾ÝµÄÒ»ÏîÒªº¦²Ù×÷¡£ÓÃÓÚ»ñÈ¡Á½¸ö±íÖоßÓÐÏà¹ØÁÐÖµµÄÐС£
Óï·¨£º
SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;
µÇ¼ºó¸´ÖÆ
ÀàÐÍ£º
INNER JOIN (ÄÚÅþÁ¬)£º½ö·µ»ØÁ½¸ö±íÖоßÓÐÆ¥ÅäÐеÄÊý¾Ý¡£
LEFT JOIN (×óÍâÅþÁ¬)£º·µ»Ø×ó±íÖеÄËùÓÐÐУ¬×ÝÈ»ÔÚÓÒ±íÖÐûÓÐÆ¥ÅäµÄÐС£
RIGHT JOIN (ÓÒÍâÅþÁ¬)£º·µ»ØÓÒ±íÖеÄËùÓÐÐУ¬×ÝÈ»ÔÚ×ó±íÖÐûÓÐÆ¥ÅäµÄÐС£
FULL JOIN (È«ÍâÅþÁ¬)£º·µ»ØÁ½¸ö±íÖеÄËùÓÐÐУ¬×ÝȻûÓÐÆ¥ÅäµÄÐС£
Ó÷¨£º
È·¶¨ÅþÁ¬ÁУºÑ¡ÔñÁ½¸ö±íÖоßÓÐÏà¹ØÖµ£¨Í¨³£ÊÇÖ÷¼ü»òÍâ¼ü£©µÄÁС£
Ö¸¶¨ JOIN ÀàÐÍ£ºÆ¾Ö¤ËùÐèµÄÆ¥ÅäÐм¯£¬Ñ¡ÔñÊʵ±µÄ JOIN ÀàÐÍ¡£
ON ×Ӿ䣺ʹÓà ON ×Ó¾äÖ¸¶¨ÅþÁ¬Ìõ¼þ¡£
Ñ¡ÕªÒª·µ»ØµÄÁУºÊ¹Óà SELECT Óï¾äÖ¸¶¨Òª´Óÿ¸ö±íÖзµ»ØµÄÁС£
ʾÀý£º
¼ÙÉèÎÒÃÇÓÐÒÔÏÂÁ½¸ö±í£º
Customers ±í£º°üÀ¨¿Í»§ÐÅÏ¢£¬¾ßÓÐ customer_id Ö÷¼ü¡£
Orders ±í£º°üÀ¨¶©µ¥ÐÅÏ¢£¬¾ßÓÐ customer_id Íâ¼ü¡£
Òª»ñÈ¡¿Í»§ÏêϸÐÅÏ¢ÒÔ¼°ÓëÆä¹ØÁªµÄËùÓж©µ¥£¬¿ÉÒÔʹÓÃÒÔÏ INNER JOIN£º
SELECT * FROM Customers INNER JOIN Orders ON Customers.customer_id = Orders.customer_id;
µÇ¼ºó¸´ÖÆ
˞񾣼
½«Êý¾Ý´Ó¶à¸ö±íºÏ²¢µ½Ò»¸öЧ¹û¼¯ÖС£
ïÔÌÈßÓàºÍÊý¾Ý·×ÆçÖ¡£
Ìá¸ßÅÌÎÊÐÔÄÜ£¬ÓÉÓÚ JOIN Ò»´ÎÐÔ¼ìË÷ËùÓÐÏà¹ØÊý¾Ý¡£
ÒÔÉϾÍÊÇmysqlÖÐjionÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡