【问题标题】:Multiple joins into the same table (TSQL)多个连接到同一个表(TSQL)
【发布时间】:2018-07-06 09:21:19
【问题描述】:

我需要 Employee 表中 OwnerID 字段和 GMID 字段中具有空值的行。

    SELECT b.FirstName + space(1) + b.LastName AS OwnerName,
           c.FirstName + space(1) + c.LastName AS GeneralManager
     FROM Store a 
        JOIN Employee b ON a.OwnerID = b.EmployeeID
        JOIN Employee c ON a.GMID = c.EmployeeID

查询正在运行...但存储表的 OwnerID 和 GMID 字段中有空值。我还需要进行哪些更改才能恢复空行?

更新 这是更正后的查询。

   SELECT b.FirstName + space(1) + b.LastName AS OwnerName,
          c.FirstName + space(1) + c.LastName AS GeneralManager  
   FROM Store a 
     LEFT JOIN Employee b ON a.OwnerID = b.EmployeeID
     LEFT JOIN Employee c ON a.GMID = c.EmployeeID

https://docs.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-2017

【问题讨论】:

    标签: sql tsql join left-join multiple-join-rows


    【解决方案1】:

    使用left join 而不是join

    LEFT JOIN 关键字返回左表 (table1) 中的所有记录,以及右表 (table2) 中的匹配记录。如果没有匹配,则从右侧开始,结果为 NULL。

    连接类型:查看更多here

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 2018-12-19
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多