Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL triple-join question

SQL triple-join question

Scheduled Pinned Locked Moved Database
questiondatabasesharepointcollaborationtutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GregStevens
    wrote on last edited by
    #1

    This is a question about the best way to construct a query. Each person is associated with a team, and each team is associated with an office. This is represented in three tables: a table of users (tblUsers) that have an id (userID) and a team (userTeamID) a table of teams (tblTeams) that have an id (teamID) and an office (teamOfficeID) a table of offices (tblOffices) that have an id (officeID). How can I (with a single query) retrieve a list of all of the people (in tblUsers) who are in a particular office (officeID)? Initially I tried this (in this example, I'm finding all the people in the office with officeID of XYZ):

    SELECT userID 
      FROM tblUsers,tblTeams,tblOffices 
      WHERE
         officeID='XYZ'
         teamOfficeID=officeID
         userTeamID=teamID
    

    But that doesn't seem to be working for me. Any thoughts? ---Greg

    I H J 3 Replies Last reply
    0
    • G GregStevens

      This is a question about the best way to construct a query. Each person is associated with a team, and each team is associated with an office. This is represented in three tables: a table of users (tblUsers) that have an id (userID) and a team (userTeamID) a table of teams (tblTeams) that have an id (teamID) and an office (teamOfficeID) a table of offices (tblOffices) that have an id (officeID). How can I (with a single query) retrieve a list of all of the people (in tblUsers) who are in a particular office (officeID)? Initially I tried this (in this example, I'm finding all the people in the office with officeID of XYZ):

      SELECT userID 
        FROM tblUsers,tblTeams,tblOffices 
        WHERE
           officeID='XYZ'
           teamOfficeID=officeID
           userTeamID=teamID
      

      But that doesn't seem to be working for me. Any thoughts? ---Greg

      I Offline
      I Offline
      Ian Dennis
      wrote on last edited by
      #2

      Isn't it : SELECT userID FROM tblUsers,tblTeams,tblOffices WHERE officeID='XYZ' AND teamOfficeID=officeID AND userTeamID=teamID ?

      1 Reply Last reply
      0
      • G GregStevens

        This is a question about the best way to construct a query. Each person is associated with a team, and each team is associated with an office. This is represented in three tables: a table of users (tblUsers) that have an id (userID) and a team (userTeamID) a table of teams (tblTeams) that have an id (teamID) and an office (teamOfficeID) a table of offices (tblOffices) that have an id (officeID). How can I (with a single query) retrieve a list of all of the people (in tblUsers) who are in a particular office (officeID)? Initially I tried this (in this example, I'm finding all the people in the office with officeID of XYZ):

        SELECT userID 
          FROM tblUsers,tblTeams,tblOffices 
          WHERE
             officeID='XYZ'
             teamOfficeID=officeID
             userTeamID=teamID
        

        But that doesn't seem to be working for me. Any thoughts? ---Greg

        H Offline
        H Offline
        Hayder Marzouk
        wrote on last edited by
        #3

        Hi, Many ways to write the query : 1- using "IN" : select userid from tblusers where userTeamId in (select teamId from tblTeams where teamOfficeId in (select officeID from tblOffices where officeId= 'XYZ')) 2- Using Join (more recommended) : SELECT tblUsers.userID FROM tblUsers INNER JOIN tblTeams ON tblUsers.userTeamId = tblTeams.teamId INNER JOIN tblOffices ON tblTeams.teamOfficeId = tblOffices.officeId where tblOffices.OfficeId = 'XYZ' HTH. Hayder Marzouk

        1 Reply Last reply
        0
        • G GregStevens

          This is a question about the best way to construct a query. Each person is associated with a team, and each team is associated with an office. This is represented in three tables: a table of users (tblUsers) that have an id (userID) and a team (userTeamID) a table of teams (tblTeams) that have an id (teamID) and an office (teamOfficeID) a table of offices (tblOffices) that have an id (officeID). How can I (with a single query) retrieve a list of all of the people (in tblUsers) who are in a particular office (officeID)? Initially I tried this (in this example, I'm finding all the people in the office with officeID of XYZ):

          SELECT userID 
            FROM tblUsers,tblTeams,tblOffices 
            WHERE
               officeID='XYZ'
               teamOfficeID=officeID
               userTeamID=teamID
          

          But that doesn't seem to be working for me. Any thoughts? ---Greg

          J Offline
          J Offline
          Jaiprakash M Bankolli
          wrote on last edited by
          #4

          Always use ANSCII standards for writing queries this means even joins, instead of where clause condition first join the table and put where clause for specific requirement.

          Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups