Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql将电话号码拆分为可识别字段_Mysql - Fatal编程技术网

mysql将电话号码拆分为可识别字段

mysql将电话号码拆分为可识别字段,mysql,Mysql,我的电话号码存储在我的用户表中的phone列下。它们被格式化为一整串数字,没有破折号。像这样:8005550012 是否有办法获取电话号码,并将其拆分为3个字段,如区号、前缀、和号码 我尝试使用类似于选择子字符串索引(子字符串索引(电话,,,,,,,,,,,,,,,,,,,,1)作为区域代码,等等的东西,但它并不漂亮,我认为它根本不正确。MID()会像这样为您做: create table test (phone varchar(50)); insert into test values ('8

我的电话号码存储在我的用户表中的
phone
列下。它们被格式化为一整串数字,没有破折号。像这样:8005550012

是否有办法获取电话号码,并将其拆分为3个字段,如
区号
前缀
、和
号码

我尝试使用类似于
选择子字符串索引(子字符串索引(电话,,,,,,,,,,,,,,,,,,,,1)作为区域代码,
等等的东西,但它并不漂亮,我认为它根本不正确。

MID()
会像这样为您做:

create table test (phone varchar(50));
insert into test values ('8005550012');
select 
  mid(phone, 1, 3) as areacode, 
  mid(phone, 4, 3) as prefix, 
  mid(phone, 7, 4) as phonenumber 
from test;

Result:
areacode |  prefix | phonenumber
800      |   555   |     0012

它们总是这样格式化吗?否
+18005550012
(800)5550012
等?是的,始终如此。没有国际代码。您的子字符串解决方案就是要使用的解决方案。您可以编写3个函数来完成这项工作,这样您的SQL语句看起来更容易理解。SUBSTRING()也应该可以工作:当我使用它时,它只选择整个电话号码。我好像没弄好,太美了!非常感谢你。