通常情况下,作为开发人员比较了解一些常用的SQL语句,也知道什么时候可以用,但有的时候对于一般性的问题,完全不用在前端来处理,用SQL语句就可以轻松应对。 下面是用纯SQL语句对两个表的数据进行的检查:
人员基础信息表:SQL语句说明select 记录总数=count(*) from memberbaseinfo20353条记录 鉴定结果表:SQL语句 说明select 记录总数=count(*) from jdjg总记录数:48581select distinct c02 as "姓名" from jdjg不同的姓名:19383 select distinct c01 as 身份证号码 from jdjg不同的身份证号码:18104select distinct c01,c02 from jdjgC01和c02组合不同:21920select c01 from jdjg group by c01 having count(*)>1有重复[身份证号码]记录数:12319select c02 from jdjg group by c02 having count(c02)>1有重复[姓名]的记录数:11758select c01,count(distinct c02) from jdjg group by c01 having count(c01)>1 and count(distinct c02)>1身份证号码相同而姓名不同的身份证号码个数:2697select c01 身份证号码,c02 姓名 from jdjg where c01 in ( select c01 from jdjg group by c01 having count(c01)>1 and count(distinct c02)>1) order by c01列出身份证号码相同而姓名不同的记录明细:12093select c02,count(distinct c01) from jdjg group by c02 having count(c02)>1 and count(distinct c01)>1姓名相同而身份证号码不同的记录:1262select c01,max(c23) from jdjg group by c01 order by c01找出每个人最新的鉴定时间:18104select c01 姓名,c19 考试分数,c22 鉴定结果,c23 鉴定时间 from jdjg where c23>'2012-1-1'找出今年鉴定的记录:4630select c01 身份证号码,c02 姓名,c19 鉴定成绩,c22 鉴定结果,c23 鉴定时间 from jdjg where c23>'2012-1-1' and c19 is null找出今年以来没有填写成绩的记录:524select c01 身份证号码,c02 姓名,c19 鉴定成绩,c22 鉴定结果,c23 鉴定时间 from jdjg where c23>'2012-1-1' and c19'2012-1-1' and c19>=60列出今年以来合格的人员名单:2899select c02 from jdjg where c01 in ( select top 100 c01 from jdjg where c23>'2012-1-1' group by c01 having count(*)>=2 order by count(*) DESC) and c23>'2012-1-1' group by c02今年以来考过两次以上的人员:321、检查在鉴定结果表中存在的身份证号码在人员基础信息表中不存在的记录数SQL语句说明select count(*) from jdjg where c01 not in ( select c01 from memberbaseinfo )4866条记录select c01,c02 from jdjg where c01 not in ( select c01 from memberbaseinfo ) order by c01列出明细记录2、检查在鉴定结果表中存在的姓名在人员基础信息表中不存在的记录数select count(*) from jdjg where c02 not in ( select c02 from memberbaseinfo )9878条记录select c01,c02 from jdjg where c02 not in ( select c02 from memberbaseinfo ) order by c01列出明细3、检查在鉴定结果表和人员基础信息表中单位名称不一致的情况select a.c01,a.c02,a.c03+a.c04+a.c05 "人事信息中的单位",b.c03+b.c04+b.c05 "鉴定结果表中的单位" from memberbaseinfo a ,jdjg b where a.c03+a.c04+a.c05b.c03+b.c04+b.c05 and a.c01=b.c0113350条记录4、对身份证号码的检查select c01,c02 from jdjg where patindex('%[吖-座]%',c01)>0包含汉字Select c01,c02 from jdjg where patindex('%[a-zA-Z]%',c01)>0包含字符5、对姓名的检查select c01,c02 from jdjg where patindex('%[0-9]%',c02)>0包含数字Select c01,c02 from jdjg where patindex('%[a-zA-Z]%',c02)>0包含字符