将同一个表连接到多个字段

时间:2011-06-29 14:08:24

标签: sql inner-join

我有这个SQL,我正在尝试将人员登录名表添加到包含多个用户ID的任务表中

  SELECT Task.TaskID
      ,Project.Project
      ,Task.Task
      ,Task.Description
      ,Task.OwnerLoginID //shown as Login.UserName
      ,Task.SubmitterID //shown as Login.UserName
      ,Task.IsVisible
  FROM Task
  INNER JOIN Project ON Task.ProjectID = Project.ProjectID
  /*
  INNER JOIN Login ON Task.SubmitterID = Login.LoginID
  INNER JOIN Login ON Task.OwnerLoginID = Login.LoginID
  */
  WHERE IsVisible = 1

我在底部的注释掉的行中出错,我哪里错了?

2 个答案:

答案 0 :(得分:8)

尝试为表分配别名,例如

SELECT Task.TaskID
      ,Project.Project
      ,Task.Task
      ,Task.Description
      ,submitted.UserName
      ,owner.UserName
      ,Task.IsVisible
  FROM Task
  INNER JOIN Project ON Task.ProjectID = Project.ProjectID
  INNER JOIN Login submitted ON Task.SubmitterID = submitted.LoginID
  INNER JOIN Login owner ON Task.OwnerLoginID = owner.LoginID
  WHERE IsVisible = 1

答案 1 :(得分:3)

如果你想加入同一个表,就像这样给出

  INNER JOIN Login L ON Task.SubmitterID = L.LoginID
  INNER JOIN Login LA ON Task.OwnerLoginID = LA.LoginID