SQL triple-join question
-
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
-
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
Isn't it :
SELECT userID FROM tblUsers,tblTeams,tblOffices WHERE officeID='XYZ' AND teamOfficeID=officeID AND userTeamID=teamID
? -
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
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
-
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
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