https://stackoverflow.com/a/11177086/1947965
从上面的正确答案链接我想跟进另一个问题。因为我想要做的就是当这组字段为空时更新字段集,否则更新到另一组字段而不仅仅是单个列。 这是另一种情况:
1。)如果subjectColumnA, timeColumnA, dataColumnA
是NULL
更新字段。
2。)如果subjectColumnA, timeColumnA, dataColumnA
为Not NULL
且subjectColumnB, timeColumnB, dataColumnB
为NULL
,则更新字段为subjectColumnB, timeColumnB, dataColumnB
等等。 。
来自@ ravinder的查询我想问这个场景: 让我们说这是表格的内容
+------+------+------+------+------+------+------+------+------+------+
| id | AAA1 | BBB1 | CCC1 | AAA2 | BBB2 | CCC2 | AAA3 | BBB3 | CCC3 |
+------+------+------+------+------+------+------+------+------+------+
| 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+------+------+------+------+
从上面的条件我提到'AAA1,BBB1和CCC1'是NULL
所以这些列将会更新。结果将是这样的。
+------+------+------+------+------+------+------+------+------+------+
| id | AAA1 | BBB1 | CCC1 | AAA2 | BBB2 | CCC2 | AAA3 | BBB3 | CCC3 |
+------+------+------+------+------+------+------+------+------+------+
| 1 | 2 | 2 | 2 | NULL | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+------+------+------+------+
另一个例子是当'AAA1,BBB1,CCC1'为'NOT NULL'时,'AAA2,BBB2和CCC2'将被更新,其结果将如下所示。
+------+------+------+------+------+------+------+------+------+------+
| id | AAA1 | BBB1 | CCC1 | AAA2 | BBB2 | CCC2 | AAA3 | BBB3 | CCC3 |
+------+------+------+------+------+------+------+------+------+------+
| 1 | 3 | 3 | 3 | NULL | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+------+------+------+------+
有可能吗?因为我从别人那里读到的问题和答案使用CASE和If仅使用2个参数。我尝试过像这样的查询,但它失败了。
UPDATE table SET
AAA3 = IF( AAA2 IS NOT NULL, 2, AAA3 ) AND BBB3 = IF( BBB2 IS NOT NULL, 2, BBB3 ),
AAA2 = IF( AAA1 IS NOT NULL, 2, AAA2 ) AND BBB2 = IF( BBB1 IS NOT NULL, 2, BBB2 ),
AAA1 = IF( AAA1 IS NULL, 1, AAA1 ) AND BBB1 = IF( AAA1 IS NULL, 1, BBB1 );
答案 0 :(得分:1)
如果没有示例数据和所需的输出,真的很难回答这个问题,但是使用您提供的链接和上面的伪语法,您应该能够以完全相同的方式执行此操作:
UPDATE TableName SET
subjectColumnA = ( case when subjectColumnA is null
AND timeColumnA is null
AND dataColumnA is null
then ?
else subjectColumnA end ),
timeColumnA = ( case when subjectColumnA is null
AND timeColumnA is null AND
dataColumnA is null
then ?
else timeColumnA end ),
dataColumnA = ( case when subjectColumnA is null AND
timeColumnA is null AND
dataColumnA is null
then ?
else dataColumnA end ),
subjectColumnB = ( case when subjectColumnA is not null AND
timeColumnA is not null AND
dataColumnA is not null AND
subjectColumnB is null AND
timeColumnB is null AND
dataColumnB is null
then ?
else subjectColumnB end ),
timeColumnB = ( case when subjectColumnA is not null AND
timeColumnA is not null AND
dataColumnA is not null AND
subjectColumnB is null AND
timeColumnB is null AND
dataColumnB is null
then ?
else timeColumnB end ),
dataColumnB = ( case when subjectColumnA is not null AND
timeColumnA is not null AND
dataColumnA is not null AND
subjectColumnB is null AND
timeColumnB is null AND dataColumnB is null
then ?
else dataColumnB end )
真的,只需添加您的条件,如果不符合,则在您的其他地方更新回自己。
- 编辑
使用上面的查询,您仍然可以使用CASE - 工作方式与IIF相同:
UPDATE table SET
AAA3 = CASE WHEN AAA2 IS NOT NULL THEN 2 ELSE AAA3 END,
BBB3 = CASE WHEN BBB2 IS NOT NULL THEN 2 ELSE BBB3 END,
AAA2 = CASE WHEN AAA1 IS NOT NULL THEN 2 ELSE AAA2 END,
BBB2 = CASE WHEN BBB1 IS NOT NULL THEN 2 ELSE BBB2 END,
...
祝你好运。