2014-03-06 58 views
2

串的UPDATE第一個字符我想更新的列的第一個字符在MYSQLMYSQL - 列

WHERE the format of the string is LIKE '1-%' 

變化的第一個字符爲「0」。

我創造了這個例子,認爲我是在正確的軌道......

UPDATE products 
SET 
    has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 0, 1),'0') 
WHERE 
    has_roundel like '1-%'; 
+2

確定,所以問題是什麼?錯誤? – Rohan

+0

這聽起來好像你可能試圖操縱嚴重的非規格化數據... – eggyal

回答

2

的MySQL SUBSTRING心不是從0

開始可以使用:

UPDATE products 
SET 
    has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 1, 1),'0') 
WHERE 
    has_roundel like '1-%'; 

或者:

UPDATE products 
SET 
    has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 1),'0') 
WHERE 
    has_roundel like '1-%'; 
1

嘗試以下操作:

update products 
set 
    has_roundel = concat('0', substring(has_roundel, 2)) 
where 
    has_roundel like '1-%'; 
1

試試這個

UPDATE products 
SET 
    has_roundel = REPLACE(has_roundel, LEFT(has_roundel,1),'0') 
WHERE 
    has_roundel like '1-%'; 

或者:

UPDATE products 
    SET 
     has_roundel = Concat('0',SUBSTR(data, 2)) 
    WHERE 
     has_roundel like '1-%'; 
0

的一種方法是利用CONCAT

UPDATE products 
SET 
    has_roundel = CONCAT('0-', substring(has_roundel, 3)) 
WHERE 
    has_roundel like '1-%';