USE AdventureWorks2016; GO SELECT StateProvinceID, AddressID FROM Person.Address WHERE AddressID IN (SELECT AddressID FROM Person.Address WHERE StateProvinceID = 39); GO
下面是结果集:
StateProvinceID AddressID ----------- ----------- 39 942 39 955 39 972 39 22660 (4 row(s) affected)
也可以使用自联接:
USE AdventureWorks2016; GO SELECT e1.StateProvinceID, e1.AddressID FROM Person.Address AS e1 INNER JOIN Person.Address AS e2 ON e1.AddressID = e2.AddressID AND e2.StateProvinceID = 39; GO
由于自联接的表会以两种不同的角色出现,因此必须有表别名 e1 和e2。 别名也可用于在内部查询和外部查询中引用同一表的嵌套查询。