如何编写显示如下结果的查询
1 Beverages NULL NULL
1 Beverages Chai Exotic Liquids
1 Beverages Cheng Exotic Liquids
1 Dairy Products NULL NULL
1 Beverages Gorgonzola Telino Tokyo Traders
1 Beverages Geitost Tokyo Traders
1 Beverages Gudbrandsdalsost Tokyo Traders
我使用了Northwind,我将编写正常的查询
SELECT c.CategoryId, c.CategoryName, p.ProductName,s.CompanyName
FROM Categories c
INNER JOIN Products p ON c.CategoryId = p.CategoryId
INNER JOIN Suppliers s ON s.SupplierId = p.CategoryId
答案 0 :(得分:1)
您可以使用该查询获得所需的结果:
SELECT c.CategoryId, c.CategoryName, p.ProductName,s.CompanyName
FROM Categories c
LEFT JOIN Products p ON c.CategoryId = p.CategoryId
LEFT JOIN Suppliers s ON s.SupplierId = p.SupplierID
UNION ALL
SELECT c.CategoryId, c.CategoryName, NULL, NULL
FROM Categories c
ORDER BY CategoryId, ProductName