查询从左侧更新第一个字符串

时间:2014-06-23 07:52:08

标签: mysql

如何更新列中包含1的字符串。

#Select column from table
sample column: 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008

#How to update one at a time query?
update column: E00001, E00002, E00003, E00004, E00005, E00006, E00007, E00008

2 个答案:

答案 0 :(得分:0)

如果您的值未以逗号分隔存储,则可以使用substring()获取1之后的字符串,并在where子句中使用left()函数来检查值应该为1作为起始字符

update t 
set `column` = CONCAT('E',SUBSTRING(`column`,2))
where left(`column` , 1) ='1'

Demo

根据评论进行修改

update t 
set `column` = CONCAT(left(`column` , 1),'E',SUBSTRING(`column`,3))
where right(left(`column` , 2),1) ='1'

Demo 2

答案 1 :(得分:0)

试试这个

UPDATE table
SET `column` = CONCAT(
                   REPLACE(
                      LEFT(`column`,1), '1', 'E'),      
                      SUBSTRING(`column`, 2));