mysqlÖеÄjoinÊÇʲô
mysql ÖÐµÄ join ÊÇÓÃÓÚ×éºÏ²î±ð±íÊý¾ÝµÄÅÌÎÊÏÂÁËüͨ¹ýÆ¥ÅäÁн¨ÉèÔÝʱ±í¡£join ÓÐËÄÖÖÀàÐÍ£ºinner join£¨½öÆ¥ÅäÁ½±íÖж¼±£´æµÄÐУ©¡¢left join£¨Ñ¡Ôñ×ó±íËùÓÐÐУ©¡¢right join£¨Ñ¡ÔñÓÒ±íËùÓÐÐУ©ºÍ full join£¨Ñ¡ÔñÁ½±íËùÓÐÐУ©¡£join ¿Éͨ¹ý×éºÏÊý¾Ý¡¢×èÖ¹×ÓÅÌÎÊ¡¢¼ò»¯ÅÌÎʵȷ½·¨Ìá¸ßЧÂʺͿɶÁÐÔ¡£
ʲôÊÇ MySQL ÖÐµÄ JOIN
JOIN ÊÇ MySQL ÖÐÓÃÓÚ×éºÏÀ´×Ô²î±ð±íµÄÊý¾ÝµÄÅÌÎÊÏÂÁî¡£Ëüͨ¹ý½ÏÁ¿Á½¸ö»ò¶à¸ö±íÖеÄÏàÆ¥ÅäÁÐÀ´½¨ÉèÔÝʱ±í£¬´Ó¶øÔÊÐíÎÒÃÇ´Ó¶à¸ö±íÖмìË÷Êý¾Ý¡£
JOIN µÄÀàÐÍ
ÓÐËÄÖÖÖ÷ÒªµÄ JOIN ÀàÐÍ£¬Ã¿ÖÖÀàÐͶ¼Æ¾Ö¤Æ¥ÅäÌõ¼þÒÔ²î±ðµÄ·½·¨×éºÏÊý¾Ý£º
INNER JOIN£º½öÑ¡ÔñÔÚËùÓÐÅþÁ¬±íÖж¼¾ßÓÐÆ¥ÅäÐеļͼ¡£
LEFT JOIN£ºÑ¡Ôñ×ó±íÖеÄËùÓмͼ£¬×ÝÈ»ËüÃÇÔÚÓÒ±íÖÐûÓÐÆ¥ÅäµÄÐС£
RIGHT JOIN£ºÑ¡ÔñÓÒ±íÖеÄËùÓмͼ£¬×ÝÈ»ËüÃÇÔÚ×ó±íÖÐûÓÐÆ¥ÅäµÄÐС£
FULL JOIN£¨»ò FULL OUTER JOIN£©£ºÑ¡ÔñÀ´×Ô×ó±íºÍÓÒ±íµÄËùÓмͼ£¬×ÝÈ»ËüÃÇÔÚÁíÒ»±íÖÐûÓÐÆ¥ÅäµÄÐС£
JOIN Óï·¨
ÒÔÏÂÊÇÒ»Ìõ INNER JOIN ÅÌÎʵÄÓï·¨£º
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
µÇ¼ºó¸´ÖÆ
SELECT£ºÖ¸¶¨Òª¼ìË÷µÄÁС£
FROM£ºÖ¸¶¨ÒªÅþÁ¬µÄ±í¡£
INNER JOIN£ºÖ¸¶¨ JOIN ÀàÐÍ¡£
ON£ºÖ¸¶¨ÅþÁ¬Ìõ¼þ¡£
JOIN µÄÀûÒæ
JOIN ºÜÊÇÓÐÓã¬ÓÉÓÚËüÔÊÐíÎÒÃÇ£º
½«Êý¾Ý×éºÏÀ´×Ô¶à¸ö±í¡£
×èÖ¹×ÓÅÌÎÊ£¬´Ó¶øÌá¸ßÐÔÄÜ¡£
¼ò»¯ÖØ´óÅÌÎÊ£¬Ê¹Æä¸üÒ×ÓÚÃ÷È·¡£
ʾÀý
˼Á¿ÒÔÏÂÁ½¸ö±í£º
¿Í»§±í£º°üÀ¨¿Í»§ÐÅÏ¢£¬Èç¿Í»§ ID¡¢ÐÕÃû¡¢µØµã¡£
¶©µ¥±í£º°üÀ¨¶©µ¥ÐÅÏ¢£¬È綩µ¥ ID¡¢¿Í»§ ID¡¢²úÆ·Ãû³Æ¡£
ÎÒÃÇ¿ÉÒÔʹÓà JOIN À´»ñȡÿ¸ö¿Í»§¼°Æ䶩µ¥ÐÅÏ¢£º
SELECT * FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;
µÇ¼ºó¸´ÖÆ
Ч¹û½«°üÀ¨ÒÔÏÂÊý¾Ý£º
customer_id | name | address | order_id | product_name |
---|---|---|---|---|
1 | John Doe | 123 Main St | 100 | Product A |
1 | John Doe | 123 Main St | 200 | Product B |
2 | Jane Smith | 456 Elm St | 300 | Product C |
ÒÔÉϾÍÊÇmysqlÖеÄjoinÊÇʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡