简体   繁体   中英

How to add values from one field in a table to another field in a different table using a common field?

I have 2 tables. I want to update a field in Table2 with the values from another field in Table1 by using BNo field as the link between the two tables.

Table 1

Bill No    Fee
25454747   4.67
25376488   6.54
23526777   3.22

Table2

BNo        Fees
12456436   NULL
21415262   NULL
12426547   NULL
23526777   NULL

Here, I need to copy the value of Fees from Table1 and put it in Table2 against the correct BillNo .

BillNo in Table1 and BNo in Table2 has the same values but they are not in the same order.

You can use UPDATE with JOIN :

UPDATE  T2
SET     T2.[Fee] = T1.[Fee]
FROM    [Table2] T2
INNER JOIN [Table1] T1
        ON T1.[BillNo] = T2.[BNo]

您可以使用UPDATE

UPDATE Table2 SET Fees = (SELECT Fee FROM Table1 WHERE BillNo = BNo)

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