官术网_书友最值得收藏!

OUTER JOIN

Outer joins are used to return all the data in one table, plus any matching rows in the other table. In the left outer join, the entire left table is returned along with any matching rows from the right table. If there is no matching row on the right, null values will be returned for these columns:

Building on the preceding example, there are some rows in the Product table that are not currently in inventory; therefore, there are no rows with these product IDs in the ProductInventory table. To return all the products, whether they have matching rows in the ProductInventory table or not, a left outer join can be used.

In this case, rows with no inventory will return NULL for the Quantity column:

SELECT Name AS ProductName, Quantity
FROM Production.Product
LEFT OUTER JOIN Production.ProductInventory ON Product.ProductID = ProductInventory.ProductID;

So, following on from what was covered previously in the SELECT clause section, NULL can be replaced by zeros in the results, if desired, by using an expression:

SELECT Name AS ProductName, ISNULL(Quantity, 0) AS Quantity
FROM Production.Product
LEFT OUTER JOIN Production.ProductInventory ON Product.ProductID = ProductInventory.ProductID;

In a right outer join, all the rows from the right table are returned along with any matching rows from the left table (and NULL for the left columns if no match exists):

If there are products in the ProductInventory table that are not in the Products table for some reason, a right outer join would return all the quantities whether they have a corresponding name or not, and a NULL value for the Name column if no matching row exists in the Products table:

SELECT Name AS ProductName, Quantity
FROM Production.Product
RIGHT OUTER JOIN Production.ProductInventory ON Product.ProductID = ProductInventory.ProductID;

In this case, it might be good to display the ProductID column if the Name column is null:

SELECT ISNULL(Name, ProductInventory.ProductID) AS ProductName, Quantity
FROM Production.Product
RIGHT OUTER JOIN Production.ProductInventory ON Product.ProductID = ProductInventory.ProductID;

For full outer joins, all rows are returned from both tables; if rows are matched, they are combined into a single row in the results:

Using a full outer join, all the rows from both the Product and the ProductInventory tables will be returned. The Name column will be NULL for rows that appear only in the ProductInventory table, and the Quantity column will be NULL for rows that appear only in the Product table:

SELECT ISNULL(Name, ProductInventory.ProductID) AS ProductName, ISNULL(Quantity, 0) AS Quantity
FROM Production.Product
FULL OUTER JOIN Production.ProductInventory ON Product.ProductID = ProductInventory.ProductID;
In all the preceding examples, SQL Server can recognize the intended type of join is outer, even if the OUTER clause is not present. For example, writing LEFT JOIN instead of LEFT OUTER JOIN.
主站蜘蛛池模板: 咸阳市| 常州市| 五河县| 武清区| 石门县| 甘谷县| 五大连池市| 五河县| 襄樊市| 宁明县| 兰州市| 新平| 泰州市| 九江县| 元谋县| 靖江市| 巴马| 建平县| 卢湾区| 西藏| 阿拉尔市| 安西县| 青神县| 济宁市| 抚顺县| 西乌珠穆沁旗| 交口县| 三河市| 洛浦县| 丹凤县| 东平县| 高清| 泸定县| 长岛县| 乾安县| 嘉峪关市| 桂林市| 江源县| 九龙县| 阿鲁科尔沁旗| 武安市|