根据多个其他记录的值更新记录的可见性

时间:2017-09-18 12:09:36

标签: sql sql-server sql-update

我在存储过程中使用以下查询,当cvFrom.value为1,2或3时,将cvUpd.visible更新为0或1.可以看出我使用OptionRestriction表来确定configvalue中的哪个optionvalues应该是是否可见,取决于configvalue中其他optionvalues的值是1还是0(我只包括1' s和0' s表中的值以保持示例简单)。

UPDATE cvUpd
SET cvUpd.visible = case cvFrom.value when 1 then 0 when 2 then 0 when 3 then 0 else 1 end 
FROM ConfigValue cvUpd
INNER JOIN OptionRestriction r ON cvUpd.ConfigurationID=67571
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1
   AND cvFrom.FeatureID = r.Feature_ID_1
   AND cvFrom.value=r.value

这是我的OptionRestriction表:

+---------------+---------------+-------+--------------+---------------+---------+
| Feature_ID_1  | OptionValue_1 | value | Feature_ID_2 | OptionValue_2 | visible |
+---------------+---------------+-------+--------------+---------------+---------+
|           399 |             6 |     1 |          403 |             2 |       0 |
|           399 |             6 |     0 |          403 |             2 |       1 |
|           399 |            14 |     1 |          403 |             2 |       0 |
|           399 |            14 |     0 |          403 |             2 |       1 |
|           776 |             1 |     1 |          403 |             2 |       0 |
|           776 |             1 |     0 |          403 |             2 |       1 |
+---------------+---------------+-------+--------------+---------------+---------+

创建上表的代码是:

CREATE TABLE OptionRestriction(
   Feature_ID_1  INTEGER  NOT NULL
  ,OptionValue_1 INTEGER  NOT NULL
  ,value         INTEGER  NOT NULL
  ,Feature_ID_2  INTEGER  NOT NULL
  ,OptionValue_2 INTEGER  NOT NULL
  ,visible       BIT  NOT NULL
);
INSERT INTO OptionRestriction(Feature_ID_1,OptionValue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (399,6,1,403,2,0);
INSERT INTO OptionRestriction(Feature_ID_1,OptionValue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (399,6,0,403,2,1);
INSERT INTO OptionRestriction(Feature_ID_1,OptionValue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (399,14,1,403,2,0);
INSERT INTO OptionRestriction(Feature_ID_1,OptionValue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (399,14,0,403,2,1);
INSERT INTO OptionRestriction(Feature_ID_1,OptionValue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (776,1,1,403,2,0);

这是我的ConfigValue表:

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     1 |       1 |
|           67571 |       399 |          14 |     0 |       1 |
|           67571 |       776 |           1 |     0 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

创建上表的代码是:

CREATE TABLE ConfigValue(
   ConfigurationID INTEGER  NOT NULL
  ,FeatureID       INTEGER  NOT NULL
  ,OptionValue     INTEGER  NOT NULL
  ,Value           INTEGER  NOT NULL
  ,Visible         BIT  NOT NULL
);
INSERT INTO ConfigValue(ConfigurationID,FeatureID,OptionValue,Value,Visible) VALUES (67571,399,6,1,1);
INSERT INTO ConfigValue(ConfigurationID,FeatureID,OptionValue,Value,Visible) VALUES (67571,399,14,0,1);
INSERT INTO ConfigValue(ConfigurationID,FeatureID,OptionValue,Value,Visible) VALUES (67571,776,1,0,1);
INSERT INTO ConfigValue(ConfigurationID,FeatureID,OptionValue,Value,Visible) VALUES (67571,403,2,0,0);

我的查询存在的问题是,现在只有功能399的optionvalue 6才能将optionvalue 2中的可见值从功能403更新为0或1,具体取决于其值是0还是1。

我实际想要实现的是当来自功能403的选项值6和14以及功能776中的选项值1的值为0时,将选项值2中的可见值从功能403更新为1,如下所示:

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     0 |       1 |
|           67571 |       399 |          14 |     0 |       1 |
|           67571 |       776 |           1 |     0 |       1 |
|           67571 |       403 |           2 |     0 |       1 |
+-----------------+-----------+-------------+-------+---------+

当来自特征399和776的选项值6,14,1的一个或多个值更新为1时,来自特征403的选项值2的可见值应更新为0,如:

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     1 |       1 |
|           67571 |       399 |          14 |     0 |       1 |
|           67571 |       776 |           1 |     0 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     0 |       1 |
|           67571 |       399 |          14 |     1 |       1 |
|           67571 |       776 |           1 |     0 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     0 |       1 |
|           67571 |       399 |          14 |     0 |       1 |
|           67571 |       776 |           1 |     1 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     1 |       1 |
|           67571 |       399 |          14 |     1 |       1 |
|           67571 |       776 |           1 |     0 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     0 |       1 |
|           67571 |       399 |          14 |     1 |       1 |
|           67571 |       776 |           1 |     1 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

+-----------------+-----------+-------------+-------+---------+
| ConfigurationID | FeatureID | OptionValue | Value | Visible |
+-----------------+-----------+-------------+-------+---------+
|           67571 |       399 |           6 |     1 |       1 |
|           67571 |       399 |          14 |     1 |       1 |
|           67571 |       776 |           1 |     1 |       1 |
|           67571 |       403 |           2 |     0 |       0 |
+-----------------+-----------+-------------+-------+---------+

如何使用我的更新查询以及OptionRestriction和ConfigValue表来实现上述目标?

0 个答案:

没有答案