GroupId
CompanyId
UserId
I need to query the users getting the company's and group's names, but I only know how to join one table. Example:
Select UserId, GroupId, Groups.Name, CompanyId, Companies.Name
From Users JOIN Groups ON Users.GroupId = Groups.Id
Hon can I add the companies table in the Join ?
Thanks,
MosheYou can have multiple JOIN clauses. I find it helpful to put them on separate lines, plus I always indicate the JOIN type since I never can remember which type is the default, and it is liable to change on someone's whim in the future. Plus normally I'd want to return the User's information whether or not I was able to successfully find the corresponding Group or Company -- which means OUTER JOINs:
SELECT
Users.UserId,
Users.GroupId,
ISNULL(Groups.Name,'**unknown**'),
Users.CompanyId,
ISNULL(Companies.Name,'**unknown**')
FROM
Users
LEFT OUTER JOIN
Groups ON Users.GroupId = Groups.Id
LEFT OUTER JOIN
Companies ON Users.CompanyID = Companies.CompanyID
Terri|||Thanks,
It was helpfull
No comments:
Post a Comment