首页 > 解决方案 > 添加列说明工作人员来自的城市

问题描述

我需要加入一些表格以显示同一记录的 3 个城市列都可以不同。到目前为止我已经得到

查询是

select a.propertyNo, a.city as propertyCity, b.city
from PropertyForRent a
inner join Branch b on a.branchNo = b.branchNo
where a.ownerNo = 'CO69'

我需要获取工作人员所在的城市,而不是分支机构编号,并且我不确定如何仅根据当前查询的分支机构编号来实现它。我可以通过自己的查询来获得答案。我只是不确定如何将它与上面的查询结合起来。

select branch.city, * from Staff
inner join Branch on staff.branchNo = branch.branchNo
where staff.staffNo = 'SA9'

如果它有助于更​​有意义,这是一个表格图

标签: sqlsql-server-2008

解决方案


对现有查询使用 PropertyForRent 的另一个联接

    select p.city  from Staff s
    inner join Branch b
   on s.branchNo = b.branchNo
   join  PropertyForRent p on p.branchNo=b.branchNo
   join  Branch bb  s.branchNo=bb.branchNo
    where staff.staffNo = 'SA9'

推荐阅读