MySQL是一種常用的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),可以用來校驗居民身份證。
CREATE FUNCTION `checkID`(idNum VARCHAR(18)) RETURNS BOOLEAN BEGIN DECLARE re CHAR(1); DECLARE p1 INT DEFAULT 0; DECLARE p2 INT DEFAULT 0; DECLARE p3 INT DEFAULT 0; DECLARE p4 INT DEFAULT 0; DECLARE p5 INT DEFAULT 0; DECLARE p6 INT DEFAULT 0; DECLARE p7 INT DEFAULT 0; DECLARE p8 INT DEFAULT 0; DECLARE p9 INT DEFAULT 0; DECLARE p10 INT DEFAULT 0; DECLARE p11 INT DEFAULT 0; DECLARE p12 INT DEFAULT 0; DECLARE p13 INT DEFAULT 0; DECLARE p14 INT DEFAULT 0; DECLARE p15 INT DEFAULT 0; DECLARE p16 INT DEFAULT 0; DECLARE p17 INT DEFAULT 0; DECLARE p18 INT DEFAULT 0; DECLARE WEIGHT CHAR(18) DEFAULT '7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2'; IF LENGTH(idNum)<>18 THEN SET re='0'; ELSEIF idNum REGEXP '^[0-9]{17}[0-9Xx]$'<>1 THEN SET re='0'; ELSE SET p1=SUBSTR(idNum,1,1)*SUBSTR(WEIGHT,1,1); SET p2=SUBSTR(idNum,2,1)*SUBSTR(WEIGHT,2,1); SET p3=SUBSTR(idNum,3,1)*SUBSTR(WEIGHT,3,1); SET p4=SUBSTR(idNum,4,1)*SUBSTR(WEIGHT,4,1); SET p5=SUBSTR(idNum,5,1)*SUBSTR(WEIGHT,5,1); SET p6=SUBSTR(idNum,6,1)*SUBSTR(WEIGHT,6,1); SET p7=SUBSTR(idNum,7,1)*SUBSTR(WEIGHT,7,1); SET p8=SUBSTR(idNum,8,1)*SUBSTR(WEIGHT,8,1); SET p9=SUBSTR(idNum,9,1)*SUBSTR(WEIGHT,9,1); SET p10=SUBSTR(idNum,10,1)*SUBSTR(WEIGHT,10,1); SET p11=SUBSTR(idNum,11,1)*SUBSTR(WEIGHT,11,1); SET p12=SUBSTR(idNum,12,1)*SUBSTR(WEIGHT,12,1); SET p13=SUBSTR(idNum,13,1)*SUBSTR(WEIGHT,13,1); SET p14=SUBSTR(idNum,14,1)*SUBSTR(WEIGHT,14,1); SET p15=SUBSTR(idNum,15,1)*SUBSTR(WEIGHT,15,1); SET p16=SUBSTR(idNum,16,1)*SUBSTR(WEIGHT,16,1); SET p17=SUBSTR(idNum,17,1)*SUBSTR(WEIGHT,17,1); IF SUBSTR(WEIGHT,18,1)='X' THEN SET p18=10*1; ELSE SET p18=SUBSTR(idNum,18,1)*SUBSTR(WEIGHT,18,1); END IF; IF MOD(p1+p2+p3+p4+p5+p6+p7+p8+p9+p10+p11+p12+p13+p14+p15+p16+p17+p18,11)=1 THEN SET re='1'; ELSE SET re='0'; END IF; END IF; RETURN re; END
通過以上代碼,我們可以在MySQL中創(chuàng)建一個名為checkID的校驗函數(shù)。在調(diào)用這個函數(shù)時,只需將需要校驗的身份證號傳遞給函數(shù)即可。