简体   繁体   中英

SQL Add values to rows from another table

I'm having problems getting a SQL query to insert data into a column from another table. Whenever I do it it adds the date as extra rows rather than in the rows that match.

Here is an example:

TABLE A

ID DATE WEIGHTING
971 3/6/21
972 3/6/21
973 3/6/21

TABLE B

ID DLTID WEIGHTING
4441 971 .03
4443 972 .05
4445 973 .01

Join TABLE A on B.DTLID = A.ID

I need to populate the A.Weighting column with values from B.Weighting, matched on A.ID = B.DLTID. The tables have millions of rows. Hope this makes sense.

Thanks!

update TableA
set WEIGHTING = B.WEIGHTING
from tableB B
where B.DTLID = TableA.ID

and to improve the performance of this update, you can add an index on TableB (DTLID, WEIGHTING)

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