mysqlÖÐÄÚÅþÁ¬,×óÅþÁ¬ºÍÓÒÅþÁ¬µÄÇø±ð
mysql ÖÐÄÚÅþÁ¬¡¢×óÅþÁ¬ºÍÓÒÅþÁ¬µÄÇø±ðÔÚÓÚ£ºÄÚÅþÁ¬Ö»·µ»ØͬʱÔÚÁ½¸ö±íÖÐÆ¥ÅäµÄÐУ¬¶ø×óÅþÁ¬·µ»Ø×ó±íËùÓÐÐУ¬°üÀ¨Æ¥ÅäÓÒ±íÐУ¬ÓÒÅþÁ¬·µ»ØÓÒ±íËùÓÐÐУ¬°üÀ¨Æ¥Åä×ó±íÐС£ÄÚÅþÁ¬Óï·¨£ºselect * from table1 inner join table2 on table1.column1 = table2.column2;×óÅþÁ¬Óï·¨£ºselect * from table1 left join table2 on table1.column1 = table2.column2;ÓÒÅþÁ¬Óï·¨£ºsele
MySQL ÖÐÄÚÅþÁ¬¡¢×óÅþÁ¬ºÍÓÒÅþÁ¬µÄÇø±ð
ÄÚÅþÁ¬ (INNER JOIN)
Ö»·µ»ØÁ½¸ö±íÖоßÓÐÆ¥ÅäÐеļͼ¡£
Æ¥Åäʧ°ÜµÄ¼Í¼½«±»ÑïÆú¡£
×óÅþÁ¬ (LEFT JOIN)
·µ»Ø×ó±íÖеÄËùÓмͼ£¬×ÝÈ»ÓÒ±íÖÐûÓÐÆ¥ÅäµÄÐС£
ÓÒ±íÖÐûÓÐÆ¥ÅäÐеļͼ½«Óà NULL ÖµÌî³ä¡£
ÓÒÅþÁ¬ (RIGHT JOIN)
·µ»ØÓÒ±íÖеÄËùÓмͼ£¬×ÝÈ»×ó±íÖÐûÓÐÆ¥ÅäµÄÐС£
×ó±íÖÐûÓÐÆ¥ÅäÐеļͼ½«Óà NULL ÖµÌî³ä¡£
Ó÷¨
ÄÚÅþÁ¬£ºÓÃÓÚ²éÕÒÁ½¸ö±íÖ®¼ä¾ßÓÐÆ¥ÅäÐеļͼ£¬²¢ÑïÆú²»Æ¥ÅäµÄ¼Í¼¡£
×óÅþÁ¬£ºÓÃÓÚ²éÕÒ×ó±íµÄËùÓмͼ£¬²¢°üÀ¨ÓÒ±íÖÐÆ¥ÅäÐеļͼ¡£
ÓÒÅþÁ¬£ºÓÃÓÚ²éÕÒÓÒ±íµÄËùÓмͼ£¬²¢°üÀ¨×ó±íÖÐÆ¥ÅäÐеļͼ¡£
Óï·¨
ÄÚÅþÁ¬£º
SELECT * FROM table1 INNER JOIN table2 ON table1.column1 = table2.column2;
µÇ¼ºó¸´ÖÆ
×óÅþÁ¬£º
SELECT * FROM table1 LEFT JOIN table2 ON table1.column1 = table2.column2;
µÇ¼ºó¸´ÖÆ
ÓÒÅþÁ¬£º
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column1 = table2.column2;
µÇ¼ºó¸´ÖÆ
Àý×Ó
¼ÙÉèÎÒÃÇÓÐÒÔÏÂÁ½¸ö±í£º
Table1: | id | name | |---|---| | 1 | John | | 2 | Mary | | 3 | Bob | Table2: | id | address | |---|---| | 1 | 123 Main St | | 2 | 456 Elm St | | 4 | 789 Oak St |
µÇ¼ºó¸´ÖÆ
ÄÚÅþÁ¬£º
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
×óÅþÁ¬£º
SELECT * FROM Table1 LEFT JOIN Table2 ON Table1.id = Table2.id;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
3 | Bob | NULL |
ÓÒÅþÁ¬£º
SELECT * FROM Table1 RIGHT JOIN Table2 ON Table1.id = Table2.id;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
id | name | address |
---|---|---|
1 | John | 123 Main St |
2 | Mary | 456 Elm St |
4 | NULL | 789 Oak St |
ÒÔÉϾÍÊÇmysqlÖÐÄÚÅþÁ¬,×óÅþÁ¬ºÍÓÒÅþÁ¬µÄÇø±ðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡