Home > Database > Mysql Tutorial > body text

How to parse ID card in mysql

藏色散人
Release: 2020-11-03 09:38:35
Original
2128 people have browsed it

Mysql method of parsing ID cards: first open the command window; then execute the SQL statement "select case left(id_number,2)when '11' then 'Beijing City'...from t_users limit 10" Get user attributes.

How to parse ID card in mysql

Recommended: "mysql video tutorial"

mysql obtains user attribute information based on ID card information

Required: Obtain user attributes based on ID card information

Method: You can use the following sql statement:

select  case left(id_number,2) 
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '未知'     
end   as 省份 ,
year(curdate())-if(length(id_number)=18,substring(id_number,7,4),if(length(id_number)=15,concat('19',substring(id_number,7,2)),null)) as 年龄, 
case if(length(id_number)=18, cast(substring(id_number,17,1) as UNSIGNED)%2, if(length(id_number)=15,cast(substring(id_number,15,1) as UNSIGNED)%2,3)) 
when 1 then '男'
when 0 then '女'
else '未知'
end as 性别
from t_users limit 10
Copy after login

The result is as follows:

+--------------------------+--------+--------+
| 省份                     | 年龄   | 性别   |
+--------------------------+--------+--------+
| 广东省                   |     40 | 男     |
| 新疆维吾尔自治区         |     25 | 男     |
| 广东省                   |     22 | 男     |
| 浙江省                   |     24 | 女     |
| 广东省                   |     38 | 男     |
| 未知                     |   NULL | 未知   |
| 河南省                   |     25 | 男     |
| 广东省                   |     25 | 女     |
| 江西省                   |     37 | 女     |
| 广东省                   |     28 | 女     |
+--------------------------+--------+--------+
10 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How to parse ID card in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template