如果column1为空,则移位列数据,并使其他列为NULL

时间:2014-08-08 00:16:55

标签: mysql sql

我有多列的表格。我想使用PHP和MYSQL编写UPDATE查询,如果只有Col1数据为NULL,则会移动列数据。 例如:

原始表

+ -------------- + -------------- + --------------- + -------------- +
| COL1 | COL2 | COL3 | COL4 |
+ -------------- -------------- + --------------- + + --- ----------- +
| NULL | DAT1 | DAT2 | DAT3 |
+ -------------- -------------- + --------------- + + --- ----------- +
| NULL | DAT1 | NULL | DAT3 |
+ -------------- -------------- + ------------- + - + -------------- +
| DAT0 | DAT1 | NULL | DAT3 |
+ -------------- -------------- + ------------- + - + -------------- +
| NULL | NULL | DAT2 | DAT3 |
+ -------------- -------------- + --------------- + + --- ----------- +

输出表格将在

之下

+ -------------- + -------------- + --------------- + -------------- +
| COL1 | COL2 | COL3 | COL4 |
+ -------------- -------------- + --------------- + + --- ----------- +
| DAT1 | DAT2 | DAT3 | NULL |
+ -------------- -------------- + --------------- + + --- ----------- +
| DAT1 | DAT3 | NULL | NULL |
+ -------------- -------------- + ------------- + - + -------------- +
| DAT0 | DAT1 | NULL | DAT3 |
+ -------------- -------------- + ------------- + - + -------------- +
| DAT2 | DAT3 | NULL | NULL |

3 个答案:

答案 0 :(得分:2)

col1NULL时,您似乎想要将所有值都移到左侧。你可以用强力逻辑来做到这一点,这对于四列来说并不是那么糟糕:

UPDATE table
  SET Col1 = coalesce(Col2, col3, col4),
      col2 = (case when col2 is not null then coalesce(col3, col4)
                   when col3 is not null then col4
              end),
      col3 = (case when col2 is not null and col3 is not null then col4 end),
      col4 = NULL
  WHERE Col1 IS NULL;

答案 1 :(得分:1)

一个php脚本,适用于任意列的列,但每行运行1次更新,因此如果你有很多行,它会很慢。

$query = "SELECT * FROM myTable WHERE Col1 IS NULL";
$rs = mysqli_query($query);

while($r = mysqli_fetch_assoc($rs)) {

    $non_null = array_filter($r);
    $update = array();

    foreach($r as $key => $value) {        
        $update[$key] = current($non_null);
        next($non_null);
    }

    $updateQuery = "UPDATE myTable SET ";
    $comma = '';

    foreach($update as $key => $value) {
        if(false === $value) {
            $updateQuery .= " $comma $key = NULL ";
        }
        else {
            $updateQuery .= " $comma $key = '$value' ";
        }
        $comma = ',';
    }

    $updateQuery .= " WHERE ";
    $and = '';

    foreach($r as $key => $value) {
        if(is_null($value)) {
            $updateQuery .= " $and $key IS NULL ";
        }
        else {
            $updateQuery .= " $and $key = '$value' ";
        }
        $and = 'AND';
    }

    mysqli_query($updateQuery);
}

答案 2 :(得分:0)

SELECT
    Phone1_Business = MIN(CASE WHEN y.rn = 1 THEN y.val END),
    Phone2_Business = MIN(CASE WHEN y.rn = 2 THEN y.val END),
    Phone3_Business = MIN(CASE WHEN y.rn = 3 THEN y.val END),
    Phone4_Business = MIN(CASE WHEN y.rn = 4 THEN y.val END),
    Phone5_Business = MIN(CASE WHEN y.rn = 5 THEN y.val END),
    Phone6_Business = MIN(CASE WHEN y.rn = 6 THEN y.val END),
    Phone7_Business = MIN(CASE WHEN y.rn = 7 THEN y.val END),
    Phone8_Business = MIN(CASE WHEN y.rn = 8 THEN y.val END),
    Phone9_Cell = MIN(CASE WHEN y.rn = 9 THEN y.val END),
    Phone10_Cell = MIN(CASE WHEN y.rn = 10 THEN y.val END),
    Phone11_Cell = MIN(CASE WHEN y.rn = 11 THEN y.val END),
    Phone12_Cell = MIN(CASE WHEN y.rn = 12 THEN y.val END),
    Phone13_Cell = MIN(CASE WHEN y.rn = 13 THEN y.val END),
    Phone14_Cell = MIN(CASE WHEN y.rn = 14 THEN y.val END),
    Phone15_Other = MIN(CASE WHEN y.rn = 15 THEN y.val END)
FROM #temp1 
OUTER APPLY
( SELECT
x.val,
rn = ROW_NUMBER() OVER (ORDER BY rn)
FROM
( VALUES 
(Phone1_Business,1), (Phone2_Business,2), (Phone3_Business,3), (Phone4_Business,4), (Phone5_Business,5), (Phone6_Business,6)
, (Phone7_Business,7), (Phone8_Business,8), (Phone9_Cell,9), (Phone10_Cell,10), (Phone11_Cell,11), (Phone12_Cell,12)
, (Phone13_Cell,13), (Phone14_Cell,14), (Phone15_Other,15)
) x (val, rn) 
WHERE x.val IS NOT NULL
) y 
GROUP BY 
#temp1.RecordID ;