在这里我们统一使用的是18位的新身份证,15位的忽略不做判断!
实现功能前,我们需要知道:18位的新身份证,倒数第二位是单数的为男,双数的为女。
知道这个后,我们就可以直接从SQL语句上着手去编写,并不需要从编程语言上写相关逻辑,如果项目还有其他的需求,那我们另说。
我的功能需求就是通过身份证计算男女比例,项目使用SSM框架,Oracle数据库。SQL语句如下:
select
count(decode(mod (to_number( substr(t.identity ,17, 1)),2 ),0, '','男' ))as man,
count(decode(mod (to_number( substr(t.identity ,17, 1)),2 ),0, '女','' ))as woman
from cmu_userdevice e
LEFT JOIN cmu_subscriber a ON a.COMMUNITYKEY =e.COMMUNITYKEY
LEFT JOIN CMU_CUSTOMER t ON t.SUBSCRIBERKEY = a.SUBSCRIBERKEY
LEFT JOIN CMU_COMMUNITY c ON c.COMMUNITYKEY = a.COMMUNITYKEY
RIGHT OUTER JOIN
(select * from CMU_USERS start with USER_ID = #{userid,jdbcType=VARCHAR} connect by prior USER_ID = PID) f ON e.USER_ID = f.USER_ID
WHERE
<if test="type == 1">
c.PROVINCEID = #{id, jdbcType=VARCHAR}
</if>
<if test="type == 2">
c.cityid = #{id, jdbcType=VARCHAR}
</if>
<if test="type == 3">
c.areaid = #{id, jdbcType=VARCHAR}
</if>
<if test="type == 4">
c.ADDRESSID = #{id, jdbcType=VARCHAR}
</if>
AND t.identity is NOT NULL
注明:大家只需要看以下两句sql
//t.identity就是存身份证的列,替换成你们数据库的列就OK
count(decode(mod (to_number( substr(t.identity ,17, 1)),2 ),0, '','男' ))as man,
count(decode(mod (to_number( substr(t.identity ,17, 1)),2 ),0, '女','' ))as woman
其他的SQL语句是其他的需求和表结构需要这么编写。