简体   繁体   中英

Update TableA from TableB Match

I've got two tables, the first column match on both tables and I need to UPDATE TableA with all the data from TableB, ONLY where TableA.Column1 and TableB.Column1 match. I'm having a really hard time wrapping my head around how to get this to work... TableA has about 80+ columns and TableB has around 100+. I'm attempting this in Microsoft Access.

TableA

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    2    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    3    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    4    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+

TableB

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    2    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    3    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    4    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+

End Result of TableA

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    2    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    3    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    4    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+

What I've Tried so far...

UNION which I quickly gave up on... It seems as that was only giving me a query, where as I need to actually UPDATE TableA with ALL the data from TableB

And also...

UPDATE TableA ([Column1],[Column2],[Column3],[Column4], ....... [ColumnX])
SELECT [Column1],[Column2],[Column3],[Column4], ....... [ColumnX]
  FROM TableB
 WHERE (TableA.Column1 = TableB.Column2)

EDIT 1:

Tried this...

Run-time error '3144': Syntax error in UPDATE statement

SQL = "   UPDATE tbleVendorData AS A " & _
      "     JOIN tbleOLD AS B " & _
      "       ON A.[Column1] = B.[Column1] " & _
      "      SET A.[Column14] = CONCAT(A.[Column14], B.[Column5])"

EDIT 2:

在此处输入图片说明

Following query may work:

UPDATE TableA 
INNER JOIN TableB 
 ON(TableA.Column1 = TableB.Column2)
SET 
    TableA.Column3 = TableB.column3,
    ...
    TableA.Columnx = TableB.columnx
Where TableA.Column1 = TableB.column1;

Hope it helps!

Have a play with:

UPDATE TableA AS A JOIN TableB AS B ON A.Column1 = B.Column1 SET A.Column1 = CONCAT(A.Column1,B.Column1) ...

Or in MSSQL

UPDATE A SET A.[Column14] = A.[Column14] + B.[Column5] FROM tbleVendorData AS A JOIN tbleOLD AS B ON A.[Column1] = B.[Column1]

If the value for any given field is blank in one table when it isn't in the corresponding field in the second, then you could try CONCAT(A.Column1,B.Column1) for example. It's not clever, but it is easier than using IF.

Unfortunately none of these suggestions work... However after doing some deep digging the below worked for me!

UPDATE TableA, TableB
   SET TableA.Column15= [TableB].[Column3]
 WHERE (([TableA].[Column1]=[TableB].[Column1]));

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