在JOIN中使用WHERE子句

时间:2018-04-23 00:12:07

标签: sql sql-server tsql

当有多个JOIN和WHERE子句只需要应用于其中一个(最后一个连接)时,如何使用where子句。我已经使用了下面的where子句,它只适用于第二次连接,但它似乎不是我正在做的方式,任何建议?

library(dplyr)
location   <- c("WA", "WA")
state      <- c("WA", "Seattle")
data       <- c(3, NA)

mydataframe <- data.frame(location, state, data)                  

mydataframefiltered <- mydataframe %>% filter(state == "WA")

2 个答案:

答案 0 :(得分:1)

TextView子句将外连接转换为内连接。您需要将其移动到适当的where子句。例如,如果on位于Position

EmployeeTime

我还强烈建议您将缩写用于表别名而不是无意义的字符。例如:

From Employee A Left Join
     EmployeeAddressInfo B
     on A.EID = B.AddId Left Join
     EmployeeRoleInfo C
     on A.EID = C.RoleID Left Join
     EmployeeTime D
     on A.EID = D.TimeID and D.Position = 'Engineer' 

答案 1 :(得分:0)

SELECT A.Name
      , B.Address
      , B.City
      , C.Position
      , C.Salary
      , D.WorkTime as Time
   From Employee as A 
   Left Join EmployeeAddressInfo as B
     on A.EID = B.AddId
   Left Join EmployeeRoleInfo as C
     on A.EID = C.RoleID and C.Position='Engineer'
   Left Join EmployeeTime as D
     on A.EID = D.TimeID