选择仅显示某些结果的视图

时间:2011-11-04 20:48:09

标签: sql sql-server tsql

我有一个使用此查询的SQL视图:

SELECT dbo.FBAShipmentItems.ShipmentID, dbo.FBAShipmentItems.FNSKU, dbo.FBAShipmentItems.ASIN, dbo.FBAShipmentItems.Merchant, dbo.FBAShipments.ShipmentName, dbo.FBAShipments.Prep_Type, dbo.FBAShipmentItems.Quantity FROM dbo.FBAShipmentItems INNER JOIN dbo.FBAShipments ON dbo.FBAShipmentItems.ShipmentID = dbo.FBAShipments.ShipmentID

问题是,当我在FBAShipmentItems中接近900个项目而在FBAShipments中有51个项目时,它只显示56个结果。如何在我的观点中获得所有900个结果

1 个答案:

答案 0 :(得分:2)

使用LEFT JOIN代替INNER JOIN

SELECT
    items.ShipmentID,
    items.FNSKU,
    items.ASIN,
    items.Merchant,
    shipments.ShipmentName,
    shipments.Prep_Type,
    items.Quantity
FROM dbo.FBAShipmentItems AS items
LEFT JOIN dbo.FBAShipments AS shipments
ON items.ShipmentID = shipments.ShipmentID

这将返回FBAShipmentItems中的所有行以及来自FBAShipments的匹配行(如果有),否则返回NULL表示该表中的所有值。