简体   繁体   中英

SQL JOIN Issue in MS Access

Having a bit of trouble with a join statement in some SQL I'm working on. Wondered if there were any smart people out there who would be able to help me with my problem!

I'm trying to JOIN 3 tables: tblMember, tblResult and tblPoint

tblMember - Containing user info such as names and emails. tblResult - Containing details of members and the races they have signed up for, as long as details of their finish positions and times they completed the race in. tblPoint - containing a set of points relating to what position a person finishes in a race, eg 1st place gets 10 points, 2nd gets 8... there are no points assigned if a person finishes 6th or below.

I want the query to return tblResult.ResultID, tblMember.MemberName, tblResult.PersonalTime, tblResult.FinishPosition, tblPoint.Points. BUT I want it to return all of these fields even if FinishPosition is empty and there are no points assigned.

I have almost solved the problem by using this SQL below:

SELECT DISTINCT tblResult.ResultID, tblMember.MemberName, tblResult.PersonalTime, 
tblResult.FinishPosition, tblPoint.Points
FROM (tblMember INNER JOIN tblResult ON tblMember.MemberID = tblResult.MemberID) LEFT   
JOIN tblPoint ON tblResult.FinishPosition = tblPoint.Position;

HOWEVER this doesn't quite work. When my query returns the results, someone who has already been assigned a position, say 2nd place, is returned multiple times with points for 1st place assigned the first time, and then the points for 2nd place assigned the second time!

This has got to be a JOIN issue... But I can't see how to rectify it! I have already tried using IIF and ISNULL but I don't think it was the right thing to use.

I'm using MS Access which appears not to support LEFT OUTER JOIN, RIGHT OUTER JOIN or FULL JOIN.

Can anyone help? Thanks for reading this. :)

EDIT: [tblPoint] contains fields (PointID, RaceTypeID, Points, Position). There are 5 rows for each race type. PointID is the primary key and the table could look like:

Point ID - 1, RaceTypeID - 1, Points - 10, Position - 1

Point ID - 2, RaceTypeID - 1, Points - 6, Position - 2

Point ID - 3, RaceTypeID - 1, Points - 3, Position - 3

Point ID - 4, RaceTypeID - 1, Points - 2, Position - 4

Point ID - 5, RaceTypeID - 1, Points - 1, Position - 5.

This means that for race type 1, whoever comes 1st gets 10 points, 2nd gets 6 points and so on. This table can support different race types, each race type with its own point system.

A member can appear any amount of times in tblResult, once a member has 'signed up' to participate they appear in tblResult, with no finish position or personal time assigned.

For anyone else that has the same problem:

I was missing another join to the tblRaceType as REMOU pointed out, thank you for that! This was my final SQL code.

SELECT tblResult.ResultID, tblMember.MemberName, tblResult.PersonalTime, 
tblResult.FinishPosition, tblPoint.Points
FROM tblRaceType RIGHT JOIN ((tblMember INNER JOIN tblResult ON tblMember.MemberID = 
tblResult.MemberID) LEFT JOIN tblPoint ON tblResult.FinishPosition = tblPoint.Position) ON 
tblRaceType.RaceTypeID = tblPoint.RaceTypeID;

MS Access had always problems mixing JOIN s and LEFT JOIN s. Split the query into two subqueries. This is the best strategy for MS Access, where you don't tweak performance anyway.

And also, if this doesn't help, try to create the queries in graphical interface since MS Access can have problems understanding the SQL source... remember, it's MS :-) Just click. Behave like MS expects - like an easy user :-)

Try these things - ie work with Access "as simple as possible" and by my experience you'll find the problem. Also with subquery stragegy (building query using simple steps) you are able to test the sub-results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM