我将电话号码存储在phone列下的users表中。它们被格式化为一个整串的数字,没有破折号。像这样: 8005550012。
有没有办法获取电话号码,并将其分成3个字段,如area_code、prefix和number
我试过使用像SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(phone, ' ', 1), ' ', -1) AS area_code,之类的东西,但是它并不漂亮,我认为它根本不正确。
发布于 2015-10-05 22:17:58
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 | 0012https://stackoverflow.com/questions/32958678
复制相似问题