sqlÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬µÄ¹Øϵ
ÔÚ sql ÖУ¬×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇÁ½ÖÖÁª½ÓÀàÐÍ£º×óÅþÁ¬ÓÅÏÈÏÔʾ×ó±íÖеÄËùÓÐÐУ¬Æ¥ÅäÐÐÏÔʾÓÒ±íÊý¾Ý£¬²»Æ¥ÅäÐÐΪ null¡£ÓÒÅþÁ¬ÓÅÏÈÏÔʾÓÒ±íÖеÄËùÓÐÐУ¬Æ¥ÅäÐÐÏÔʾ×ó±íÊý¾Ý£¬²»Æ¥ÅäÐÐΪ null¡£
SQL ÖÐ×óÅþÁ¬ºÍÓÒÅþÁ¬µÄ¹Øϵ
ÔÚ SQL ÖУ¬×óÅþÁ¬ºÍÓÒÅþÁ¬ÊÇÁ½ÖÖÓÃÓÚÅþÁ¬±íµÄÁª½ÓÀàÐÍ£¬ËüÃÇÔÚÊý¾Ý¼ìË÷ºÍÅÌÎÊÖÐÊ©Õ¹×ÅÖÁ¹ØÖ÷ÒªµÄ×÷Óá£
¹Øϵ±ÈÕÕ
ÌØÕ÷ | ×óÅþÁ¬ | ÓÒÅþÁ¬ |
---|---|---|
Æ¥ÅäÓÅÏȼ¶ | ×ó±íÓÅÏÈ | ÓÒ±íÓÅÏÈ |
Ч¹û¼¯ | °üÀ¨×ó±íÖеÄËùÓÐÐУ¬Æ¥ÅäÐÐÏÔʾÓÒ±íÊý¾Ý£¬²»Æ¥ÅäÐÐΪ NULL | °üÀ¨ÓÒ±íÖеÄËùÓÐÐУ¬Æ¥ÅäÐÐÏÔʾ×ó±íÊý¾Ý£¬²»Æ¥ÅäÐÐΪ NULL |
ÏêϸڹÊÍ
×óÅþÁ¬ (LEFT JOIN)
×óÅþÁ¬ÓÅÏÈÏÔʾ×ó±íÖеÄËùÓÐÐУ¬×ÝÈ»ÔÚÓÒ±íÖÐÕÒ²»µ½Æ¥ÅäÐС£¹ØÓÚÕÒµ½Æ¥ÅäÐеÄÐУ¬Ëü½«ÏÔʾÀ´×ÔÓÒ±íµÄÏìÓ¦Êý¾Ý¡£¹ØÓÚûÓÐÆ¥ÅäÐеÄÐУ¬Ëü½«ÏÔʾ NULL Öµ¡£
ÓÒÅþÁ¬ (RIGHT JOIN)
ÓÒÅþÁ¬Óë×óÅþÁ¬Ïà·´£¬ËüÓÅÏÈÏÔʾÓÒ±íÖеÄËùÓÐÐС£¹ØÓÚÕÒµ½Æ¥ÅäÐеÄÐУ¬Ëü½«ÏÔʾÀ´×Ô×ó±íµÄÏìÓ¦Êý¾Ý¡£¹ØÓÚûÓÐÆ¥ÅäÐеÄÐУ¬Ëü½«ÏÔʾ NULL Öµ¡£
ʾÀý
˼Á¿ÒÔÏÂÁ½¸ö±í£º
×ó±í (Customers)
CustomerID | Name |
---|---|
1 | John Smith |
2 | Mary Jones |
3 | David Wilson |
ÓÒ±í (Orders)
OrderID | CustomerID | Amount |
---|---|---|
101 | 1 | 100 |
102 | 3 | 200 |
103 | 4 | 300 |
×óÅþÁ¬ÅÌÎÊ£º
SELECT * FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
CustomerID | Name | OrderID | Amount |
---|---|---|---|
1 | John Smith | 101 | 100 |
2 | Mary Jones | NULL | NULL |
3 | David Wilson | 102 | 200 |
ÓÒÅþÁ¬ÅÌÎÊ£º
SELECT * FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
OrderID | CustomerID | Name | Amount |
---|---|---|---|
101 | 1 | John Smith | 100 |
102 | 3 | David Wilson | 200 |
103 | 4 | NULL | 300 |
ÒÔÉϾÍÊÇsqlÖеÄ×óÅþÁ¬ºÍÓÒÅþÁ¬µÄ¹ØϵµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡