2017-05-31 97 views
1

我有一個表名tariff,但現在我想在列service_codeMySQL的更新一個字段,替換第一個字符

替換值的數值是這樣的:

D2P 
D2D 
D2D 
D2P 
D2D 

我想達到什麼:

P2P 
P2D 
P2D 
P2P 
P2D 

只是改變了 'd' 到 'P',作爲第一字符

+0

您的列名在服務代碼空間? – lalithkumar

+0

不,錯字:p ..謝謝你的回答 – may

+0

隨時歡迎@ mayy – lalithkumar

回答

1

使用下面的查詢:

UPDATE tariff SET service_code=CONCAT('P', SUBSTRING(service_code FROM 2)) 
    where substring(service_code,1,1)='D'; 

UPDATE tariff SET service_code=CONCAT('P', SUBSTRING(service_code FROM 2)) 
     where left(service_code,1)='D'; 
+0

不要忘記接受這個答案@may – lalithkumar

+1

我覺得你的因爲她想更換爲多行所有的答案是不好 – hiule

+0

是的,我明白現在該怎麼辦,她預計@hiule – lalithkumar

2

使用更多通用查詢

update tariff set service_code='P'+substring(service_code,2,len(service_code)-1) 
where substring(service_code,1,1)='D' 
相關問題