1

一个简单的查询,应该查看systemSoftware表格和软件表格,并只显示softwareId不在systemSoftware表格内的内容。

到目前为止,这是我的查询...

SELECT s.softwareId AS 'Software ID',
    s.softwareDescription AS 'Software Description',
    sv.vendorName AS 'Vendor Name',
    c.cityName AS 'City Name'
FROM software AS s
JOIN systemSoftware AS ss
ON s.softwareId = ss.softwareId
JOIN softwareVendor AS sv
ON s.vendorId = sv.vendorId
JOIN city AS c
ON sv.zipCode = c.zipCode
WHERE s.softwareId NOT IN (ss.softwareId)
ORDER BY s.softwareId
GO

当我删除 NOT 时,它会softwareId显示systemSoftware.

问题是我想要相反的。那些不在systemSoftware.

4

1 回答 1

2

你需要一个左外连接:

SELECT ss.softwareId AS 'Software ID',
    s.softwareDescription AS 'Software Description',
    sv.vendorName AS 'Vendor Name',
    c.cityName AS 'City Name'
FROM software AS s
left outer JOIN systemSoftware AS ss
ON s.softwareId = ss.softwareId
left outer JOIN softwareVendor AS sv
ON s.vendorId = sv.vendorId
left outer JOIN city AS c
ON sv.zipCode = c.zipCode
WHERE ss.softwareId is NULL
ORDER BY s.softwareId

我还更改了ORDER BY, 因为按不存在的列排序是没有意义的。

于 2012-11-27T07:16:20.487 回答