为什么在两个不同的列上连接两次相同的表会返回不同的列值?

时间:2017-01-21 15:43:55

标签: sql-server tsql join

我有一个不是由我编写的存储过程。

我无法理解他们如何两次加入同一个表tblUsers并返回不同的列值:

select distinct 
    usr.Name_FirstLast AS AssignedTo,
    usr1.Name_FirstLast as AssignedBy 
from 
    dbo.tblNoteStore  nt_str
join 
    dbo.tblNoteEntities entit ON nt_str.ID = entit.NoteGUID
join
    dbo.tblNoteDiaries nt_dia ON nt_str.ID = nt_dia.NoteGUID
join
    dbo.tblNoteEntries entri on nt_str.ID = entri.NoteGUID
                             and nt_dia.EntryGUID = entri.ID 
                             and entit.NoteGUID = entri.NoteGUID
left join 
    dbo.tblNoteRecipients recip ON entri.ID = recip.EntryGUID
--this is where the same table used twice 
left join 
    dbo.tblUsers  usr ON recip.UserGUID = usr.UserGUID   -- returns AssignedTo column
left join
    dbo.tblUsers usr1 ON usr1.UserGuid = entri.UserGUID   -- returns AssignedBy column
where 
    usr.UserGUID = '55610B2F-1997-40C0-9F01-EED3ED2939F9'

结果是我需要的,但为什么会这样呢?

  • dbo.tblUsers有主键UserGUID
  • dbo.tblNoteEntries有一个外键UserGUID
  • dbo.tblNoteRecipients有一个外键UserGUID

enter image description here

另外,我是否必须使用JOIN中的所有表来接收结果?

2 个答案:

答案 0 :(得分:1)

因为用户表连接到两个不同的表。

left join 
    dbo.tblUsers  usr ON recip.UserGUID = usr.UserGUID

加入代表recip的别名dbo.tblNoteRecipients,其中为

left join
    dbo.tblUsers usr1 ON usr1.UserGuid = entri.UserGUID

加入代表entri的别名dbo.tblNoteEntries

请注意,这两个别名是在先前的连接中声明的:

                         |
join                     V
    dbo.tblNoteEntries entri on nt_str.ID = entri.NoteGUID
                             and nt_dia.EntryGUID = entri.ID 
                             and entit.NoteGUID = entri.NoteGUID
left join 
    dbo.tblNoteRecipients recip ON entri.ID = recip.EntryGUID
                            ^
                            |

答案 1 :(得分:1)

从清理语法开始,并在左侧放置相同的表

select distinct 
       usr.Name_FirstLast  AS AssignedTo 
     , usr1.Name_FirstLast as AssignedBy 
from tblNoteStore      nt_str
join tblNoteEntities   entit 
      ON entit.NoteGUID  = nt_str.ID  
join tblNoteDiaries  nt_dia 
      ON nt_dia.NoteGUID = nt_str.ID 
join tblNoteEntries    entri 
      on entri.NoteGUID = nt_str.ID 
     and entri.NoteGUID = entit.NoteGUID
     and entri.ID       = nt_dia.EntryGUID           
join tblNoteRecipients recip 
      ON recip.EntryGUID = entri.ID   
join tblUsers          usr 
      ON usr.UserGUID  = recip.UserGUID   -- returns AssignedTo column
join tblUsers          usr1 
      ON usr1.UserGuid = entri.UserGUID   -- returns AssignedBy column
where usr.UserGUID = '55610B2F-1997-40C0-9F01-EED3ED2939F9'  

这在很多层面都是愚蠢的

tblUsers是PK,绝对没有理由离开加入它

usr.UserGUID =' 55610B2F-1997-40C0-9F01-EED3ED2939F9'正在杀死左连接

这    加入tblNoteEntities权限           ON entit.NoteGUID = nt_str.ID

我们知道entit.NoteGUID = nt_str.ID
但你正在重复

 join tblNoteEntries entri 
      on entri.NoteGUID = nt_str.ID 
     and entri.NoteGUID = entit.NoteGUID  

我能做出最好的理解

select distinct 
       usr.Name_FirstLast  AS AssignedTo 
     , usr1.Name_FirstLast as AssignedBy 
from tblNoteStore      nt_str
join tblNoteEntities   entit 
      ON entit.NoteGUID  = nt_str.ID  
join tblNoteDiaries    nt_dia 
      ON nt_dia.NoteGUID = nt_str.ID 
join tblNoteEntries    entri 
      on entri.NoteGUID  = nt_str.ID 
     and entri.ID        = nt_dia.EntryGUID           
join tblNoteRecipients recip 
      ON recip.EntryGUID = entri.ID   
join tblUsers          usr 
      ON usr.UserGUID  = recip.UserGUID   -- returns AssignedTo column
     and usr.UserGUID = '55610B2F-1997-40C0-9F01-EED3ED2939F9'
join tblUsers          usr1 
      ON usr1.UserGuid = entri.UserGUID   -- returns AssignedBy column