我需要加入一些表来表明,对于相同的记录,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'

发布于 2018-09-14 08:39:48
试试这个:
select a.propertyNo
, a.city as propertyCity
, b.city as propertyBranchCity
, sb.city as staffBranchCity
from PropertyForRent a
inner join Branch b on b.branchNo = a.branchNo
inner join Staff s on s.staffNo = a.staffNo
inner join Branch sb on sb.branchNo = s.branchNo
where a.ownerNo = 'CO69'在现有查询中,您正在执行所有正确的操作。
您遇到的问题可能是需要从staff表中提取结果中没有显示的数据。注意:即使输出中没有包含来自这些表的字段,也可以使用表来创建联接。
或者您的问题是您需要两次加入branch表;一次是为了获得属性的分支,另一次是为了获得工作人员的分支。请注意,您可以随心所欲地加入同一个表;只需给每个表提供一个不同的别名(例如,上面示例中的b、sb ),以便区分它们。
发布于 2018-09-14 08:41:01
在现有查询中为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'https://stackoverflow.com/questions/52328006
复制相似问题