如何根据另一个 table 更新 table 值

How to update table value based on another table

我有一个名为 Product 的 table 及其数据和 table 结构如下所示,

+-----------+-------------+-----------+
| ProductId | ProductName | SortValue |
+-----------+-------------+-----------+
| 1157      | ABC         | 7         |
+-----------+-------------+-----------+
| 1156      | DEF         | 3         |
+-----------+-------------+-----------+
| 1155      | GHI         | 4         |
+-----------+-------------+-----------+
| 1154      | JKL         | 2         |
+-----------+-------------+-----------+
| 1153      | MNO         | 1         |
+-----------+-------------+-----------+
| 1152      | PQR         | 5         |
+-----------+-------------+-----------+
| 1151      | STU         | 6         |
+-----------+-------------+-----------+

我有另一个名为 LocationProdut 的 table,它是参考产品 table 和位置 table,每个位置 productid 都有自己的排序 value.sample 数据和 table 结构如下,

+--------------+-------------+-----------+
| fkLocationId | fkProductId | Sortvalue |
+--------------+-------------+-----------+
| 19           | 1157        | 1         |
+--------------+-------------+-----------+
| 19           | 1156        | 2         |
+--------------+-------------+-----------+
| 19           | 1155        | 3         |
+--------------+-------------+-----------+
| 19           | 1154        | 4         |
+--------------+-------------+-----------+
| 19           | 1153        | 5         |
+--------------+-------------+-----------+
| 19           | 1152        | 6         |
+--------------+-------------+-----------+
| 19           | 1151        | 7         |
+--------------+-------------+-----------+
| 20           | 1155        | 3         |
+--------------+-------------+-----------+
| 20           | 1154        | 4         |
+--------------+-------------+-----------+
| 20           | 1153        | 5         |
+--------------+-------------+-----------+
| 20           | 1152        | 6         |
+--------------+-------------+-----------+
| 20           | 1151        | 7         |
+--------------+-------------+-----------+
| 21           | 1155        | 3         |
+--------------+-------------+-----------+
| 21           | 1154        | 4         |
+--------------+-------------+-----------+
| 21           | 1153        | 5         |
+--------------+-------------+-----------+
| 21           | 1152        | 6         |
+--------------+-------------+-----------+
| 21           | 1151        | 7         |
+--------------+-------------+-----------+

现在我需要更新 LocationProdut table 中的 SortValue 库,以便仅 Product table 的 SortValue fkLocationId = 19 & 20

预期输出如下。

+------------+-------------+-----------+
| fkBranchId | fkServiceId | Sortvalue |
+------------+-------------+-----------+
| 19         | 1157        | 7         |
+------------+-------------+-----------+
| 19         | 1156        | 3         |
+------------+-------------+-----------+
| 19         | 1155        | 4         |
+------------+-------------+-----------+
| 19         | 1154        | 2         |
+------------+-------------+-----------+
| 19         | 1153        | 1         |
+------------+-------------+-----------+
| 19         | 1152        | 5         |
+------------+-------------+-----------+
| 19         | 1151        | 6         |
+------------+-------------+-----------+
| 20         | 1155        | 4         |
+------------+-------------+-----------+
| 20         | 1154        | 2         |
+------------+-------------+-----------+
| 20         | 1153        | 1         |
+------------+-------------+-----------+
| 20         | 1152        | 5         |
+------------+-------------+-----------+
| 20         | 1151        | 6         |
+------------+-------------+-----------+
| 21         | 1155        | 3         |
+------------+-------------+-----------+
| 21         | 1154        | 4         |
+------------+-------------+-----------+
| 21         | 1153        | 5         |
+------------+-------------+-----------+
| 21         | 1152        | 6         |
+------------+-------------+-----------+
| 21         | 1151        | 7         |
+------------+-------------+-----------+
UPDATE        targetTable
SET           targetTable.targetColumn = s.sourceColumn
FROM          targetTable t
INNER JOIN    sourceTable s
ON            t.matchingColumn = s.matchingColumn

您可以使用简单的 inner join 来更新所需的行:

UPDATE LP
  SET 
      LP.Sortvalue = P.SortValue
FROM LocationProduct LP
JOIN Product P ON LP.fkProductId = P.ProductId
WHERE fkLocationId IN(19, 20);

请查找数据库<>fiddle here.