Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join same table to multiple fields

Tags:

sql

inner-join

I have this SQL and i am trying to join a table of people's login names to a table of tasks that contains several user ids

  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

i get an error in the commented out lines at the bottom where am i going wrong?

like image 371
harryovers Avatar asked Jul 23 '26 19:07

harryovers


1 Answers

try assigning alias names to the tables e.g.

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
like image 104
Mark D Avatar answered Jul 25 '26 10:07

Mark D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!