Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL数据清理_Mysql_Sql - Fatal编程技术网

MySQL数据清理

MySQL数据清理,mysql,sql,Mysql,Sql,我正在尝试清理MySQL中的一些数据,我不确定修复以下问题的最有效方法。我有三列A、B和C。它们通常具有相同的值。如果A&B相同或A&C相同,那么如果可能的话,我想用非空值填写另一个选项。B&C不影响A的值。例如: ------------------- |A |B |C | ------------------- |1 |2 |3 | |1 |2 |NULL | |2 |5 |8 | |2 |NULL |8 | |3

我正在尝试清理MySQL中的一些数据,我不确定修复以下问题的最有效方法。我有三列A、B和C。它们通常具有相同的值。如果A&B相同或A&C相同,那么如果可能的话,我想用非空值填写另一个选项。B&C不影响A的值。例如:

-------------------
|A    |B    |C    |
-------------------
|1    |2    |3    |
|1    |2    |NULL |
|2    |5    |8    |
|2    |NULL |8    |
|3    |NULL |9    |
|3    |NULL |NULL |
-------------------

在上面的示例中,第2行C列应填写3,第4行B列应填写5。当我只有两个选项时,我们会相应地填写。因此,第6行、第C列应该是9,而第5行、第B列和第6行、第B列都保持为空。我如何编写一个脚本来解决这个问题,如果B或C不是NULL,那么我们将根据表中的其他值来填充它?谢谢

这有点复杂,但您可以在一条
update
语句中完成:

update table t left outer join
       table tc
       on t.a = tc.a and t.b = tc.b and tc.c is not null left outer join
       table tb
       on t.a = tb.a and t.c = tb.c and tb.b is not null
    set t.c = coalesce(t.c, tc.c),
        t.b = coalesce(t.b, tb.b);
此查询根据指定的规则进行自联接以查找新值。每个联接都会引入一个值。如果有多个匹配行,则从多个行中选择一个任意值

首先运行
select
,可以看到发生了什么:

select *
from   table t left outer join
       table tc
       on t.a = tc.a and t.b = tc.b and tc.c is not null left outer join
       table tb
       on t.a = tb.a and t.c = tb.c and tb.b is not null;



set c = (case when c is null
              then (select c from 

您可以使用
case
编写
update
语句。我们将非常乐意帮助您编写脚本,但到目前为止您尝试了什么?