简体   繁体   中英

Using MS Access how to perform an update with multiple joins and where clauses?

I am having a bit of trouble with getting some MS Access SQL to work. Here is the high level:

I have values in one table, by15official that I need to use to update related records in another table, investmentInfo . Pretty straight forward except there are quite a few joins I need to perform to make sure the right record is updated in the investmentTable and I think I could figure this out with regular sql, but Access is not playing nicely. The following is my sql I am trying to use (which results in this error: "Syntax error (missing operator) in query expression ..."

update ii
set ii.investmentType = by15.InvestmentType
from investmentInfo as ii
inner join 
    (select by15Official.InvestmentType, by15Official.InvestmentNumber
     from (((by15official left join investmentinfo on by15official.InvestmentNumber = investmentInfo.investID) 
    left join fundingSources on fundingSources.investId = investmentInfo.id)
    left join budgetInfo on budgetInfo.fundingID = fundingSources.id)
    where investmentinfo.submissionType = 2
    and budgetInfo.byYear = 2015
    and budgetInfo.type = 'X') as by15
on by15.InvestmentNumber = ii.investID

This seems like it should work, I am trying to join this group of tables that provide the investmentType which is what I want to update in the main table investmentInfo . Thoughts? Can this be done in Access? I have googled around and found the above which I adapted to meet my needs (actually I am pretty sure I found the above on SO).

Thoughts?

Thank you very much!

I did get some help from someone over at the MS forums. The solution was to format my SQL slightly differently. Here is the code that eventually worked.

UPDATE 
(
 (
   by15official LEFT JOIN investmentinfo 
   ON by15official.InvestmentNumber = investmentInfo.investID 
 )
 LEFT JOIN 
 fundingSources 
 ON investmentInfo.id = fundingSources.investId 
) 
LEFT JOIN budgetInfo 
ON fundingSources.id = budgetInfo.fundingID

SET investmentInfo.investmentType = by15official.InvestmentNumber

WHERE (investmentinfo.submissionType = 2)
And (budgetInfo.byYear = 2015)  

Perhaps the above Access SQL can help others.

Basically you want to do the update and the joins before doing the set and where clauses. Makes sense, and I am sure if I were better skilled at writing SQL I would have known that.

I know this doesn't use the original code, but this is a generic example with the proper syntax:

UPDATE ([My First Table]
LEFT JOIN [My First Table] ON [My Second Table].[RequestID] = 
    [My First Table].[ID])
INNER JOIN [My Third Table] ON [My Second Table].[Some Field] = 
    [My Third Table].[Matching Field]
SET 
 [My First Table].[Approved By] = [My Third Table].[Approver], 
 [My First Table].[Approval Date] = [My Second Table].[Modified]
WHERE [My First Table].[Review Status] LIKE '*Approved*'

The easiest way to figure out Joins in SQL in Access is go to the Query Design window, add the tables you want, and click and drag Columns to the table you want to join based on. You do this for all your tables and boom your joins are done. Also if your joins are getting too complicated it generally means there is a database design issue though I know fixing this is not always an option in production databases. Good Luck!

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