如果要在数据库里直接把手机号码前面的0去掉可以这样做update `user` set `tel`= substring(tel,2,length(tel)) where length(`tel`)=14如果只是想在查询出来的结果里把0去掉 可以这样select case when length(tel)=14 then substring(tel,2,length(tel)) else tel end tel from user
如果要在数据库里直接把手机号码前面的0去掉可以这样做update `user` set `tel`= substring(tel,2,length(tel)) where length(`tel`)=12如果只是想在查询出来的结果里把0去掉 可以这样select case when length(tel)=12 then substring(tel,2,length(tel)) else tel end tel from user
如果座机带区号也有12号的话,那就在上面的基础上 加个条件,如果是12位,则看前三位 如果是010就不替换,如果不是则替换,手机号码没有10开头且是13位的吧? select case when length(tel)=12 then substring(tel,2,length(tel)) else tel end tel from user where substring(tel,1,3) != '010'update `user` set `tel`= substring(tel,2,length(tel)) where length(`tel`)=12 and substring(tel,1,3) != '010'
直接 mysql 操作 update tbl_name set caller=substr(caller,2) where caller regexp '^01[^0]'
php实现,座机号从全国各地打来的肯定不能去掉0
$tel = <<<tel
013428504829
013578504829
057112345678
07551234567
02112345678
0101234567
tel;$tel = explode("\n", $tel);$tel_arr = array('13', '15', '18');
foreach ($tel as $key => $val)
{
if($val[0] == '0' && in_array("$val[1]$val[2]", $tel_arr))
$tel[$key][0] = '';
}
print_r($tel);
set `tel`= substring(tel,2,length(tel))
where
length(`tel`)=14如果只是想在查询出来的结果里把0去掉 可以这样select
case when length(tel)=14 then substring(tel,2,length(tel)) else tel end tel
from user
set `tel`= substring(tel,2,length(tel))
where
length(`tel`)=12如果只是想在查询出来的结果里把0去掉 可以这样select
case when length(tel)=12 then substring(tel,2,length(tel)) else tel end tel
from user
只要排除掉 010 北京区号就可以了$caller = preg_replace('/^0(1[^0])/', '$1', $caller);
如果座机带区号也有12号的话,那就在上面的基础上 加个条件,如果是12位,则看前三位 如果是010就不替换,如果不是则替换,手机号码没有10开头且是13位的吧?
select
case when length(tel)=12 then substring(tel,2,length(tel)) else tel end tel
from user
where substring(tel,1,3) != '010'update `user`
set `tel`= substring(tel,2,length(tel))
where
length(`tel`)=12 and substring(tel,1,3) != '010'
update tbl_name set caller=substr(caller,2) where caller regexp '^01[^0]'