请大家帮忙看看,下面的算法如何实现?

Carryf 2009-04-17 06:43:39
在mysql中,外部程序传入一个完整的输入的电话号码,如何能准确的去掉前缀,而把电话号码取出来?
比如电话号码为:1234567(号码可能为8位或4位或7位)
前缀有:852,3,3852,852133
假如用户输入:31234567 ,在mysql中如何判断前缀是3?
或者用户输入:8521234567 。大家帮忙看看,多谢。
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzl8146 2009-04-18
  • 打赏
  • 举报
回复
是否需要考虑输入不带前缀的电话号码?
Carryf 2009-04-18
  • 打赏
  • 举报
回复
多谢两位的指点,现在我把ACMAIN_CHM 和lzl8146 的做法,做成存储过程:
DELIMITER $$

DROP PROCEDURE IF EXISTS `crm`.`test`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `test`(DialedNumbers char(20))
begin
Declare RealDial char(20);
DECLARE RetVar char(20);

select substr(DialedNumbers,max(length(acode))+1) into RealDial
from areacode
where instr(DialedNumbers,acode)=1 ;
if not isnull(RealDial) then
set RetVar=RealDial;
else
set RetVar=DialedNumbers;
end if;

select RetVar;
end$$

DELIMITER ;

call test('85210000');
ACMAIN_CHM 2009-04-17
  • 打赏
  • 举报
回复

算法如下。

mysql> create table areacode(
-> acode varchar(10) primary key
-> )
->
-> ;
Query OK, 0 rows affected (0.09 sec)

mysql> insert into areacode values (852),(3),(3852),(852133);
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select * from areacode;
+--------+
| acode |
+--------+
| 3 |
| 3852 |
| 852 |
| 852133 |
+--------+
4 rows in set (0.00 sec)

mysql> select substr('31234567',max(length(acode))+1)
-> from areacode
-> where instr('31234567',acode)=1 ;
+-----------------------------------------+
| substr('31234567',max(length(acode))+1) |
+-----------------------------------------+
| 1234567 |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select substr('38521234567',max(length(acode))+1)
-> from areacode
-> where instr('38521234567',acode)=1 ;
+--------------------------------------------+
| substr('38521234567',max(length(acode))+1) |
+--------------------------------------------+
| 1234567 |
+--------------------------------------------+
1 row in set (0.05 sec)

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧