Why is LEFT JOIN causing a “multi-part identifier

2020-03-07 14:28发布

I'm getting the error "The multi-part identifier 'MS2.ExtraValueID' cannot be bound" when trying to run the following SQL query in Access 2010.

SELECT [Contact Details].ContactID
FROM ([Contact Details] LEFT JOIN [Extra Fields - Newsletters & Bulletins] ON [Contact Details].ContactID = [Extra Fields - Newsletters & Bulletins].ContactID) LEFT JOIN [Extra Fields - Number of Employees] ON [Extra Fields - Newsletters & Bulletins].ContactID = [Extra Fields - Number of Employees].ContactID;

ExtraValueID appears in the WHERE clause of both of the "Extra Value..." queries, but isn't outputted.

Looking up this question from a few years back, it suggests this is probably due to a typo. However, I can't see anything wrong with the above SQL, which would suggest one of the individual queries which make up this bigger query is wrong.

However, each of the queries individually runs fine, and if I only try to join one of the Extra Fields tables to it, it works fine. I thought the ampersands might be causing an issue, but replacing them with "and", or deleting them altogether makes no difference.

If I replace the LEFT JOINs with INNER JOINs, it works inasmuch as it returns results, but not the results I want, because I should return all the results from Contact Details whether there are matching records in the two Extra Fields tables or not.

SELECT [Contact Details].ContactID, [Contact Details].[Client Code], [Extra Fields - Newsletters & Bulletins].Newsletters, [Extra Fields - Number of Employees].[Number of Employees]
FROM ([Contact Details] INNER JOIN [Extra Fields - Newsletters & Bulletins] ON [Contact Details].ContactID = [Extra Fields - Newsletters & Bulletins].ContactID) INNER JOIN [Extra Fields - Number of Employees] ON [Extra Fields - Newsletters & Bulletins].ContactID = [Extra Fields - Number of Employees].ContactID;

2条回答
走好不送
2楼-- · 2020-03-07 14:59
SELECT [Contact Details].ContactID
FROM [Contact Details] LEFT JOIN [Extra Fields - Newsletters & Bulletins] ON [Contact Details].ContactID = [Extra Fields - Newsletters & Bulletins].ContactID LEFT JOIN [Extra Fields - Number of Employees] ON [Extra Fields - Newsletters & Bulletins].ContactID = [Extra Fields - Number of Employees].ContactID;

Remove brackets ( ) .

To make your query more readable use Aliases

 SELECT c.ContactID
 FROM [Contact Details] c 
 LEFT JOIN [Extra Fields - Newsletters & Bulletins] e
         ON c.ContactID = e.ContactID 
 LEFT JOIN [Extra Fields - Number of Employees] emp
         ON e.ContactID = emp.ContactID;
查看更多
Melony?
3楼-- · 2020-03-07 15:15

Solved it - Access was throwing a tantrum because I'd mixed INNER and LEFT joins, even though the INNER joins were in the constituent queries that made up the larger query and should have just been outputting results. By replacing all of the INNER joins in the two Extra Fields queries, the query takes a little longer to run, but runs correctly.

查看更多
登录 后发表回答