1

我有以下查询,它正在更新缺少连字符的 MAC 地址,但对于其他拥有它们的人来说,它正在将它们更改为空。尝试更新一列中的值。

UPDATE mytable SET MAC =
CASE
 WHEN MAC NOT LIKE '%-%' 
  THEN UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
 END;

任何建议都会很棒。

谢谢

4

2 回答 2

3

Just add an ELSE case which sets the value to its current value:

UPDATE mytable SET MAC =
CASE
 WHEN MAC NOT LIKE '%-%' 
  THEN UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
  /* Else case sets it to its current value to avoid NULL */
  ELSE MAC
 END;

It's also possible to use a WHERE clause so rows that should not be updated are not matched in the first place. That is probably the more appropriate action to take in this instance, unless you have other conditions to apply in the CASE

UPDATE mytable 
SET MAC = UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
WHERE MAC NOT LIKE '%-%'
于 2013-01-11T17:31:59.190 回答
1
UPDATE mytable 

SET MAC = UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))

WHERE  MAC NOT LIKE '%-%' 
于 2013-01-11T17:34:41.720 回答