griffard
Programmer
- Jun 28, 2009
- 3
I have been repeatedly failing to do something that seems like it should work.
I'm trying to retrieve records from my PROJECTS table based on a WHERE but I am also usong some joins to add useful columns to the resultset.
I have a table PROJECTS, that includes
a "Ref" field
a "Name" field
a "CustomerRef" field that refers to a contact
I have a table CONTACTS, that includes
a "Ref" field
a "Name"
I have a table ROLES that includes three fields
"ProjectRef" that refers to a record in PROJECTS
"Role" (name of a role, such as 'ProjectManager')
"ContactRef" that refers to a record in CONTACTS
This third table allows a many to many relationship between people and projects.
So I want to list projects along with the name of the customer and the name of the ProjectManager for each project.
SELECT
PROJ.Ref AS "Proj Ref",
PROJ.Name AS "Proj Name",
CUST.Name AS "Cust name",
PM.Name AS "Proj Manr"
FROM
PROJECTS AS PROJ
LEFT JOIN CONTACTS AS CUST ON PROJ.CustomerRef = CUST.Ref
LEFT JOIN ROLES ON (ROLES.ProjectRef = PROJ.Ref) AND
(ROLES.Role = 'ProjectManager')
LEFT JOIN CONTACTS AS PM ON ROLES.ContactRef = PM.Ref
WHERE
etc etc
I can retrieve all the records I want without the last LEFT join but I need that to display the Name (rather than the Ref) of the person who is the ProjectManager on each project.
When I leave out the PM.Name column and the last LEFT JOIN, I get all the records I expect to get.
When I add the last LEFT JOIN, I get no records at all.
Am I trying to do something LEFT JOIN cannot do ?
Is there a better way ?
Thanks in advance.
I'm trying to retrieve records from my PROJECTS table based on a WHERE but I am also usong some joins to add useful columns to the resultset.
I have a table PROJECTS, that includes
a "Ref" field
a "Name" field
a "CustomerRef" field that refers to a contact
I have a table CONTACTS, that includes
a "Ref" field
a "Name"
I have a table ROLES that includes three fields
"ProjectRef" that refers to a record in PROJECTS
"Role" (name of a role, such as 'ProjectManager')
"ContactRef" that refers to a record in CONTACTS
This third table allows a many to many relationship between people and projects.
So I want to list projects along with the name of the customer and the name of the ProjectManager for each project.
SELECT
PROJ.Ref AS "Proj Ref",
PROJ.Name AS "Proj Name",
CUST.Name AS "Cust name",
PM.Name AS "Proj Manr"
FROM
PROJECTS AS PROJ
LEFT JOIN CONTACTS AS CUST ON PROJ.CustomerRef = CUST.Ref
LEFT JOIN ROLES ON (ROLES.ProjectRef = PROJ.Ref) AND
(ROLES.Role = 'ProjectManager')
LEFT JOIN CONTACTS AS PM ON ROLES.ContactRef = PM.Ref
WHERE
etc etc
I can retrieve all the records I want without the last LEFT join but I need that to display the Name (rather than the Ref) of the person who is the ProjectManager on each project.
When I leave out the PM.Name column and the last LEFT JOIN, I get all the records I expect to get.
When I add the last LEFT JOIN, I get no records at all.
Am I trying to do something LEFT JOIN cannot do ?
Is there a better way ?
Thanks in advance.