#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Please help with sql Queries


    Hi everyone can someone please help, I am not able to run the following query. I know th error but do not know how to correct it. I am trying to display like columns from multiple tables. Here is the query I am having trouble with

    SELECT EmpTrnID, SessionID, EmployeeID from tblEmployeeTraining, tblEmployee, tblSessions
    WHERE tblEmployeeTraining.EmployeeID = tblEmployee.EmployeeID
    WHERE tblSessions.SessionID = tblEmployeeTraining.SessionID
    ORDER BY employeeID;

    When I use this code I get an Error for Colums being Ambiguous
    Which I cannot seem to get rid of.
    I know the error is for having duplicate collumns in multiple tables but I do not know how to eliminate this. the next code


    SELECT EmpTrnID, SessionID,
    e.employeeid, et.employeeid from tblEmployee e,
    tblEmployeeTraining et where e.employeeid = et.employeeid;

    I can run and gives me the results I am looking for from 2 tables but I cannot get all three tables together can someone please help me out on this. Thank you for your time,

    John
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    Apart from the ambiguous columns you have two WHERE clauses, which is wrong. You also are using the old-style implicit JOIN syntax, which is not good.

    Code:
    SELECT trn.EmpTrnID
         , ses.SessionID
         , emp.EmployeeID
      FROM tblEmployeeTraining AS trn
         INNER JOIN tblEmployee AS emp
             ON trn.EmployeeID = emp.EmployeeID
         INNER JOIN tblSessions AS ses
             ON ses.SessionID = trn.SessionID
      ORDER BY emp.employeeID
    I am making the assumption that the EmpTRnID is from the tblEmployeeTraining table.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by SimonJM
    Apart from the ambiguous columns you have two WHERE clauses, which is wrong. You also are using the old-style implicit JOIN syntax, which is not good.

    Code:
    SELECT trn.EmpTrnID
         , ses.SessionID
         , emp.EmployeeID
      FROM tblEmployeeTraining AS trn
         INNER JOIN tblEmployee AS emp
             ON trn.EmployeeID = emp.EmployeeID
         INNER JOIN tblSessions AS ses
             ON ses.SessionID = trn.SessionID
      ORDER BY emp.employeeID
    I am making the assumption that the EmpTRnID is from the tblEmployeeTraining table.



    Thank you
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by John_in_Parrish
    Thank you
    Also could you recomend any tutorials for me to get a better understanding of the combining of 3 or more tables. I am new to this and really want and need to learn it. Thank you so much for your help.

    John
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    r937 has a very good book, look at his signature for a link!

    The way to attach more tables is just to add more JOIN statements along with the ON clause to link the two tables togther. Which form of JOIN you want/need (LEFT, RIGHT, INNER, etc.,) will depend on how you want the data returned.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,146
    Rep Power
    4274
    Originally Posted by SimonJM
    r937 has a very good book, look at his signature for a link!
    thanks, simon

    simply-sql.com has been a placeholder for a while (long story) with no links

    go to sitepoint.com or amazon.com if you want to purchase a copy (sitepoint has no more paper editions, just eboooks)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    Thank you for your help!!!!

IMN logo majestic logo threadwatch logo seochat tools logo