Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

UPDATE QUERY ( using multiple joins within  update statement)

Posted on 2006-07-02
4
831 Views
Last Modified: 2008-01-09
UPDATE Query

I have three tables;

Table A

InvT      Class

1      A1
2      A2
3      A3


Table B

Class      Class_id

A1      11
A2      22
A3      33

TABLE C

Invt      Class_id

1      NULL
2      NULL
3      NULL

I want a query to update the CLass_id field in the TABLE C...

So basically need to run through and link the invt( Table C ) to Table A ( by Invt )
then link to Table B  ( by Class )to get the Class_id I need...

Can this be done with an update query ?                  
0
Comment
Question by:I_J_Evans
  • 3
4 Comments
 
LVL 84
ID: 17026698
Normally you don't store the values from other tables in a different table, instead you use the source tables and build a query which returns those values:

SELECT TableA.InvT, TableB.Class_id FROM TableA INNER JOIN TableB ON TableA.Class = TableB.Class

this would return the InvT from TableA and Class_id from TableB, which is (basically) what your TableC is showing ...

Not to say you don't sometimes need to store data from one table in another (the classic example is in an Ordering system - you alway store the Price of an item, even if that Price is stored elsewhere in the DB, since prices change during a products lifecycle) - but typically there is no need to do so, and storing data in several different places can result in different results during calculations.

Are these queries to be stored in the db? If so, you can build a "join" query to join TableA and TableB, then use that to update TableC:

[qryJoin]
SELECT TableA.InvT, TableB.Class_id
FROM TableA INNER JOIN TableB ON TableA.Class = TableB.Class;

Now build the Update query:

[qryUpdate]
UPDATE TableC SET TableC.Class_id = (SELECT TableB.Class_id FROM (TableA INNER JOIN TableB ON TableA.Class = TableB.Class) INNER JOIN TableC ON TableA.InvT = TableC.Invt);

Save both, then run qryUpdate to update TableC:

DoCmd.OpenQuery "qryUpdate"

You could probably combine these two into one.
0
 
LVL 84
ID: 17026704
Sorry, the SQL for qryUpdate in the above posting is incorrect. It should be:

[qryUpdate]
UPDATE TableC LEFT JOIN qryJoin ON TableC.Invt = qryJoin.InvT SET TableC.Class_id = qryJoin.Class_id;
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
ID: 17026723
If you want to do this all in one SQL statement, this should work:

UPDATE TableC LEFT JOIN (TableA LEFT JOIN TableB ON TableA.Class = TableB.Class) ON TableC.Invt = TableA.InvT SET TableC.Class_id = [TableB].[Class_id];

You could save this as a query, or use this directly in VBA:

Currentdb.Execute "UPDATE TableC LEFT JOIN (TableA LEFT JOIN TableB ON TableA.Class = TableB.Class) ON TableC.Invt = TableA.InvT SET TableC.Class_id = [TableB].[Class_id]"

0
 
LVL 6

Expert Comment

by:maxy88
ID: 17026740

Try this:

UPDATE [Table C] AS H1,
(SELECT [Table B].[Class_id], [Table A].[InvT]
FROM [Table C] INNER JOIN ([Table B] INNER JOIN [Table A] ON [Table B].[Class]=[Table A].[Class]) ON [Table C].[InvT]=[Table A].[InvT]) AS H2
SET [H1].[Class_id] = [H2].[Table B].[Class_id]
WHERE [H1].[InvT]=[H2].[Table A].[InvT];

Max.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question