MySQL·身份证校验
时间:2022-05-03
本文章向大家介绍MySQL·身份证校验,主要内容包括本文节选自《Netkiller Architect 手札》、4.17. 数据检查、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
本文节选自《Netkiller Architect 手札》
4.17. 数据检查
4.17.1. 身份证校验
该函数能够检查身份证号码是否正确
CREATE DEFINER=`neo`@`%` FUNCTION `check_id_number`(`idnumber` CHAR(18))
RETURNS enum('true','false')
LANGUAGE SQL
NOT DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE status ENUM('true','false') default 'false';
DECLARE verify CHAR(1);
DECLARE sigma INT;
DECLARE remainder INT;
IF length(idnumber) = 18 THEN
set sigma = cast(substring(idnumber,1,1) as UNSIGNED) * 7
+cast(substring(idnumber,2,1) as UNSIGNED) * 9
+cast(substring(idnumber,3,1) as UNSIGNED) * 10
+cast(substring(idnumber,4,1) as UNSIGNED) * 5
+cast(substring(idnumber,5,1) as UNSIGNED) * 8
+cast(substring(idnumber,6,1) as UNSIGNED) * 4
+cast(substring(idnumber,7,1) as UNSIGNED) * 2
+cast(substring(idnumber,8,1) as UNSIGNED) * 1
+cast(substring(idnumber,9,1) as UNSIGNED) * 6
+cast(substring(idnumber,10,1) as UNSIGNED) * 3
+cast(substring(idnumber,11,1) as UNSIGNED) * 7
+cast(substring(idnumber,12,1) as UNSIGNED) * 9
+cast(substring(idnumber,13,1) as UNSIGNED) * 10
+cast(substring(idnumber,14,1) as UNSIGNED) * 5
+cast(substring(idnumber,15,1) as UNSIGNED) * 8
+cast(substring(idnumber,16,1) as UNSIGNED) * 4
+cast(substring(idnumber,17,1) as UNSIGNED) * 2;
set remainder = MOD(sigma,11);
set verify = (case remainder
when 0 then '1' when 1 then '0' when 2 then 'X' when 3 then '9'
when 4 then '8' when 5 then '7' when 6 then '6' when 7 then '5'
when 8 then '4' when 9 then '3' when 10 then '2' else '/' end
);
END IF;
IF right(idnumber,1) = verify THEN
set status = 'true';
END IF;
RETURN status;
END
首先我们使用正确身份证号码进行测试,返回true
mysql> select check_id_number('330702198003090915');
+---------------------------------------+
| check_id_number('330702198003090915') |
+---------------------------------------+
| true |
+---------------------------------------+
1 row in set (0.01 sec)
长度不符合18位直接返回false.
mysql> select check_id_number('33070219800309');
+-----------------------------------+
| check_id_number('33070219800309') |
+-----------------------------------+
| false |
+-----------------------------------+
1 row in set (0.00 sec)
mysql> select check_id_number('33070219800309091457889');
+--------------------------------------------+
| check_id_number('33070219800309091457889') |
+--------------------------------------------+
| false |
+--------------------------------------------+
1 row in set, 1 warning (0.00 sec)
随便改译为数,校验失败返回 false
mysql> select check_id_number('330702198003090914');
+---------------------------------------+
| check_id_number('330702198003090914') |
+---------------------------------------+
| false |
+---------------------------------------+
1 row in set (0.00 sec)
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- Java的包机制以及import、static import的用法详解(看这篇就够了)
- AIM2020 Efficient Super Resolution: Methods and Results
- hive的group by与distinct的区别及性能测试比较
- hive的order by操作
- centos安装mysql-server报错:No package mysql-server available.
- Java的内部类详解(结合代码全面分析)
- jdk8安装及环境变量配置
- 使用MA Anderson御用软件SpliceSeq对TCGA数据库的RNA-seq找可变剪切
- Tomcat9安装配置、服务配置开机自启动以及启动窗口的中文乱码问题解决
- Java的System.exit()详解
- Hadoop伪分布式搭建(hadoop2.x通用)
- R语言tryCatch使用方法:判断Warning和Error
- leetcode1546题解【前缀和+贪心】
- leetcode560题解【前缀和+哈希】
- 5秒解决:VMware Workstation 与 Hyper-V 不兼容