根据同一列(不同行)MSSQL中的值更新列值

时间:2017-09-07 18:12:53

标签: sql-server

我无法根据同一列中的值更改列值。

如果D244的“setting_id”的“setting_value”为1,那么我还需要将“8F60更改为1

以下是数据表settings

的示例
| setting_id | setting_value |
| ---------- | ------------- |
|   D244     |       1       |
|   8F60     |       0       |
|   AD4F     |       1       |

settings

我的尝试:

UPDATE settings s, (SELECT DISTINCT setting_id, setting_value FROM settings WHERE setting_value like '1' and setting_id like 'D244') s1
SET s.settingValue = s1.settingValue
WHERE s.settingID like '8F60'

1 个答案:

答案 0 :(得分:1)

试试这个 -

UPDATE settings s INNER JOIN settings s1
ON s1.setting_id = s.setting_id
SET s.settingValue = s1.settingValue
WHERE s.settingID like '8F60'
AND s1.setting_id = 'D244'