Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relationship to Multiple Fields

Status
Not open for further replies.

djwatts

Programmer
Nov 10, 2004
91
GB
I am working on a housing application, I have two tables (of many) one is called contracts and contains four fields; ContractNo, Surveyor, QS, COW.

Surveyor, QS & COW all relate to the SurveyorID in the Surveyor table.

When I design a form I can use combo boxes etc to display the surveyors name, but when I build a query/report I can only get the name for the Surveyor field and not the other two. Do i need to use expressions or customise the inner joins in SQL???

Any help appreciated

Regards

DW
 
Surveyor, QS & COW all relate to the SurveyorID in the Surveyor table.

Then all you should be storing in your Contracts table is Surveyor ID.
Code:
Contracts                         Surveyors
ContractNo (PK)                   SurveyorID(PK)
SurveyorID (FK)                   Surveyor
                                  QS
                                  COW
Then your query becomes:

SELECT ContractNO, Surveyor FROM Contracts INNER JOIN Surveyors on Contracts.SurveyorID = Surveyors.SurveyorID

Anytime you have data duplicates in your tables you haven't finished normalizing. Check out The Fundamentals of Relational Database Design



Leslie

In times of universal deceit, telling the truth will be a revolutionary act. - George Orwell
 
Sorry I don't think I made it that clear,

Each contract can have a different Surveyor, QS and COW

(Sample Records)

[Contracts]
ContractNo (PK) = 1001
Surveyor (FK) = 01
QS (FK) = 05
COW (FK) = 08

[Surveyor]
SuveyorID (PK) = 01
Name = "Dan Watts"
---------------------
SuveyorID (PK) = 05
Name = "John Smith"
---------------------
SuveyorID (PK) = 08
Name = "James Connolly"


The fields in [Contracts] link to SurveyorID

Thanks

Dan
 
Ok then:

SELECT ContractNO, A.SurveyorName, B.SurveyorName, C.SurveyorName FROM
Contracts
INNER JOIN Surveyor As A On Contracts.SurveyorID = A.SurveyorID
INNER JOIN Surveyor As B On Contracts.SurveyorID = B.SurveyorID
INNER JOIN Surveyor As C On Contracts.SurveyorID = C.SurveyorID


Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
SELECT ContractNo, S.Name AS S_name, Q.Name AS Q_Name, C.Name AS C_Name
FROM ((Contracts A
INNER JOIN Surveyor S ON A.Surveyor = S.SuveyorID)
INNER JOIN Surveyor Q ON A.QS = Q.SuveyorID)
INNER JOIN Surveyor C ON A.COW = C.SuveyorID;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top