从表2中的列更新表1中的列

时间:2019-08-22 02:22:29

标签: sql sql-server

我有两个表:

RatingScore表

列:id,carid,design,carValue,MadeDate,TestDate

这些是我的评分得分插入值

(4,10720654,'Honda1.0_C',09.0000000000000000,'2019-06-20 00:00:00.0000000', '2019-06-20 00:00:00.0000000'),
(4,10720672,'Honda1.0_C',10.8230000000000000,'2019-06-19 00:00:00.0000000', '2019-06-19 00:00:00.0000000'),
(4,10720984,'Honda5.0',38.9250000000000000,'2019-06-18 00:00:00.0000000', '2019-06-18 00:00:00.0000000'),
(4,10720672,'Honda1.0',09.8589000000000000,'2019-06-17 00:00:00.0000000', '2019-06-17 00:00:00.0000000'),
(4,10720672,'Honda1.0',12.6780000000000000,'2019-06-16 00:00:00.0000000', '2019-06-16 00:00:00.0000000')

然后我有一个名为RatingUpdate的表

此表具有相同的列和以下值:

(4,10720654,'Honda1.0_C',05.0000000000000000,'2019-06-20 00:00:00.0000000', '2019-08-21 00:00:00.0000000'),
(4,10720672,'Honda1.0_C',06.8000000000000000,'2019-06-19 00:00:00.0000000', '2019-08-21 00:00:00.0000000'),
(4,10720984,'Honda5.0',38.9250000000000000,'2019-06-18 00:00:00.0000000', '2019-08-21 00:00:00.0000000'),
(4,10720672,'Honda1.0',05.9589000000000000,'2019-06-17 00:00:00.0000000', '2019-08-21 00:00:00.0000000'),
(4,10720672,'Honda1.0',11.4570000000000000,'2019-06-16 00:00:00.0000000', '2019-08-21 00:00:00.0000000')

我想通过从第二个表中获取Carvalues和TestDate并将其提供给第一个表来更新第一个表。第一个表中的错误,将修复它们。我不希望在设计为“ Honda5.0”的地方更新行,此表中还有许多其他记录,这只是一个示例。请提供有关如何完成此操作的示例。非常感谢

1 个答案:

答案 0 :(得分:1)

不确定您到底需要什么,但是会是这样:

UPDATE s
SET s.design = p.design
    , s.MadeDate = p.MadeDate
    , s.TestDate = p.TestDate
FROM RatingScore s
INNER JOIN RatingUpdate p ON s.id = p.id AND s.carid = p.carid
WHERE s.design <> 'Honda5.0'