|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have two tables
CREW CrewID Person1 Person2 PARTAKER PartakerID LastName Fields "Person1" and "Person2" both point to Table "PARTAKER" i am having real difficulty getting an SQL statement to retrieve both names on the same row, has anyone got any ideas on what the correct syntax is? This statement gives me two rows:- SELECT Crew.CrewID,Partaker.LastName FROM Crew, Partaker WHERE (Crew.Person1=Partaker.PartakerID) OR (Crew.Person2=partaker.partakerid) G |
|
#2
|
||||
|
||||
|
i suppose you have data in a similar way:
Code:
CrewID | Person1 | Person2
-----------------------------------------
1 Frank
2 Mike
since the two records are different, it's right to get them in two different rows. It should be wrong if you have data like this: Code:
CrewID | Person1 | Person2
-----------------------------------------
1 Frank Mike
and if you get two rows, you have to modify your query by adding a DISTINCT before SELECT. hope it's clear enough ![]() |
|
#3
|
|||
|
|||
|
No its the second one!
I have two person crews, and i want to somehow relate the two members together, i have other info in the CREW table that is unique to the crew and not to the individuals! G |
|
#4
|
||||
|
||||
|
Code:
select Crew.CrewID
, crew1.LastName
, crew2.lastname
from Crew
left outer
join Partaker crew1
on Crew.Person1 = crew1.PartakerID
left outer
join Partaker crew2
on Crew.Person2 = crew2.PartakerID
rudy http://r937.com/ |
|
#5
|
|||
|
|||
|
Rudy,
That gives me a syntax error in MS Access I also dont understand how it works can you explain please? G |
|
#6
|
||||
|
||||
|
Code:
select Crew.CrewID
, crew1.LastName
, crew2.lastname
from (
Crew
left outer
join Partaker crew1
on Crew.Person1 = crew1.PartakerID
)
left outer
join Partaker crew2
on Crew.Person2 = crew2.PartakerID
the way it works is that it joins the Crew table to two "copies" of the Partaker table rudy |
|
#7
|
|||
|
|||
|
Rudy,
Thanks, that worked a treat, i have been trying to get my head round that for about a week, and in the end it was so simple. I have bought myself a book on SQL now so hopefully i wont have any more simple questions like that one! G |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > One to Many relationship |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|