开发工具与关键技术: Java
作者:肖广斌
撰写时间:2021年5月24日

在大部分的地方我们都会用到模糊查询的功能,模糊查询的条件也是各种各样的,其中就有年龄段的筛选和时间段的筛选,那下面就来看一下怎么使用这两个条件进行模糊查询。

根据身份证计算年龄关键sql语句:
SELECT (YEAR(NOW())- SUBSTRING(表的身份证字段,7,4)) age FROM 表名;
就是计算当前年份减去从身份证字段里截取到的年份,因为从第七位开始就是年份,截取四位得到年份,然后计算得到年龄,这是18位数身份证的,15位的也大同小异。
1、首先得在mapper层写条件查询的接口方法
写一个传入多个参数作为查询条件最后返回list的方法

//多条件分页查询学员信息
List<Students> selStudents(@Param("nameIdPhone")String nameIdPhone,@Param("departmentID")Integer departmentID,@Param("namePointID")Integer namePointID,@Param("recruitStudentID")Integer recruitStudentID,@Param("driveTypeID")Integer driveTypeID,@Param("shiftClassID")Integer shiftClassID,@Param("sex")Boolean sex,@Param("startTime")String startTime,@Param("endTime")String endTime,@Param("ageID1")Integer ageID1,@Param("ageID2")Integer ageID2);

2、然后就是在xml中写sql语句
连表查询+条件判断。
关键sql语句:
1、表名.字段 between #{开始时间} and #{结束时间}
2、(YEAR(NOW())- SUBSTRING(身份证字段,7,4)) between #{年龄1} and #{年龄2}
between 在xxx和xxx范围之间

<!-- 多条件分页查询学员信息 -->
<select id="selStudents" resultType="com.jx.pojo.Students">select students .*,status.StatusName,registrationpoint.RPName,drivingtype.drivingTypeName,shiftclass.SCName,recruitstudent.RecruitName,department.DepartmentName,studentgroup.SGName from studentsleft join status on status.StatusID = students.StatusIDleft join registrationpoint on registrationpoint.RegistrationPointID = students.RegistrationPointIDleft join drivingtype on drivingtype.drivingTypeID = students.drivingTypeIDleft join shiftclass on shiftclass.ShiftClassID = students.ShiftClassIDleft join recruitstudent on recruitstudent.RecruitStudentID = students.RecruitStudentIDleft join department on department.DepartmentID = students.DepartmentIDleft join studentgroup on studentgroup.StudentGroupID = students.StudentGroupID<where><if test="nameIdPhone != null and nameIdPhone != ''" >and (students.name like concat('%',#{nameIdPhone},'%')or students.idNumber like concat('%',#{nameIdPhone},'%')or students.phoneOne like concat('%',#{nameIdPhone},'%')or students.phoneTwo like concat('%',#{nameIdPhone},'%'))</if><if test="departmentID != 0 and departmentID != ''">and department.PDepartmentID = #{departmentID}</if><if test="namePointID != 0 and namePointID != ''">and students.RegistrationPointID = #{namePointID}</if><if test="recruitStudentID != 0 and recruitStudentID != ''">and students.RecruitStudentID = #{recruitStudentID}</if><if test="driveTypeID != 0 and driveTypeID != ''">and students.drivingTypeID = #{driveTypeID}</if><if test="shiftClassID != 0 and shiftClassID != ''">and students.ShiftClassID = #{shiftClassID}</if><if test="sex != null">and students.sex = #{sex}</if><if test="startTime!=null">and students.registrationTime between #{startTime} and  #{endTime}</if><if test="endTime!=null">and students.registrationTime between #{startTime} and  #{endTime}</if><if test="ageID1 != 0 and ageID1 != ''" >and (YEAR(NOW())- SUBSTRING(idNumber,7,4)) between #{ageID1} and  #{ageID2}</if><if test="ageID2 != 0 and ageID2 != ''">and (YEAR(NOW())- SUBSTRING(idNumber,7,4)) between #{ageID1} and  #{ageID2}</if></where>
</select>

3、接着在jsp页面写条件查询的方法
获取页面参数,加入条件判断之后传到控制器

//条件查询
function CheckSignUp(){var nameIdPhone = $("#nameIdPhone").val();var namePointID = $("#NamePointID").val();var departmentID = $("#DepartmentID").val();var recruitStudentID = $("#RecruitStudentID").val();var test1 = $("#test1").val();var ageID1 = $("#AgeID1").val();var ageID2 = $("#AgeID2").val();var driveTypeID = $("#DriveTypeID").val();var shiftClassID = $("#ShiftClassID").val();var sexID = $("#SexID").val();var startTime = test1.substring(0, 10);var endTime = test1.substring(13);if (nameIdPhone == undefined || nameIdPhone == null || nameIdPhone == "") {nameIdPhone = "";}if (departmentID == "" || departmentID == undefined || departmentID == null) {departmentID = 0;}if (namePointID == "" || namePointID == undefined || namePointID == null) {namePointID = 0;}if (recruitStudentID == undefined || recruitStudentID == "" || recruitStudentID == null) {recruitStudentID = 0;}if (driveTypeID == undefined || driveTypeID == "" || driveTypeID == null) {driveTypeID = 0;}if (shiftClassID == undefined || shiftClassID == "" || shiftClassID == null) {shiftClassID = 0;}if(startTime==""||startTime==null||endTime==""||endTime==null){startTime="undefined";endTime="undefined"}else{startTime = new Date(startTime).getTime();endTime = new Date(endTime).getTime();}if (ageID1 == undefined || ageID1 == "" || ageID1 == null) {ageID1 = 0;}if (ageID2 == undefined || ageID2 == "" || ageID2 == null) {ageID2 = 0;}//表格数据重载//方法级渲染的重载tabSignUp.reload({url: "${ctx}/Students/selStudents",where: {nameIdPhone:nameIdPhone,departmentID:departmentID,namePointID:namePointID,recruitStudentID:recruitStudentID,driveTypeID:driveTypeID,shiftClassID:shiftClassID,startTime:startTime,endTime:endTime,sexID:sexID,ageID1:ageID1,ageID2:ageID2},page: {curr: 1 //重新从第 1 页开始}});
}

4、最后在控制器写查询的逻辑
接收页面传递过来的参数,传入查询方法进行数据查询,最后返回data

//条件分页查询学员信息
@ResponseBody
@RequestMapping(value = "/selStudents", produces = "application/json; charset=utf-8")
private Layui selStudents(HttpServletRequest request,String nameIdPhone,Integer departmentID,Integer namePointID,Integer recruitStudentID,Integer driveTypeID,Integer shiftClassID,String startTime,String endTime,Boolean sexID,Integer ageID1,Integer ageID2) {//获取参数String pageNum = request.getParameter("curr");String pageSize = request.getParameter("nums");if("".equals(startTime)||startTime==null||"undefined".equals(startTime)) {startTime=null;}else {startTime = new SimpleDateFormat("yyyy-MM-dd").format(Long.parseLong(startTime));}if("".equals(endTime)||endTime==null||"undefined".equals(endTime)) {endTime=null;}else {endTime = new SimpleDateFormat("yyyy-MM-dd").format(Long.parseLong(endTime));}List<Students> list = new ArrayList<>();//查询数据list = StudentsMapper.selStudents(nameIdPhone,departmentID,namePointID,recruitStudentID,driveTypeID,shiftClassID,sexID,startTime,endTime,ageID1,ageID2);//循环学员表信息for (Students student : list) {//声明变量和空字符串String str = "";//拼接车型+班制str += student.getDrivingtypename()+"-"+student.getScname();student.setCarclassname(str);//赋值}PageService<Students> stu= new PageService<Students>();PageBean<Students> pageBean = stu.findByPage(list, Integer.parseInt(pageNum),  Integer.parseInt(pageSize));return Layui.data(pageBean);
}

演示效果如下:
输入查询条件前

输入查询条件后

还有就是年龄的查询条件不能从大到小,开始年龄比结束年龄大、不能两个年龄相同、不能只写开始年龄不写结束年龄,或者只写结束年龄不写开始年龄,这样都是无法查询数据的。如下图:



Sql根据身份证计算年龄和时间范围的条件查询相关推荐

  1. SQL根据出生日期计算年龄的两种算法

    --Sql根据出生日期计算年龄 1.  select datediff(year,EMP_BIRTHDAY,getdate()) as '年龄' from  EMPLOYEEUnChangeInfo ...

  2. sql根据身份证获取年龄、性别、出生日期等信息

    #属性nl为年龄;sfzh为身份证号;xb 为性别;csrq 为出生日期 #根据身份证计算年龄并修改 update kw_test set nl= (substring(now(),1,4)-subs ...

  3. sql 根据出生日期计算年龄

    如表sample的字段xm对应姓名,csrq对应出生日期要计算年龄     String   sql   =   "select   xm,   to_char(sysdate,'YYYY' ...

  4. MySQL基础——数据库和SQL概述\MySQL基本使用\DQL语言学习\条件查询\排序查询\常见函数\分组查询\连接查询\子查询\分页查询\联合查询

    本文详细讲解了MySQL中DQL语言,也就是数据查询语句的使用.全文3w余字,是对学习MySQL知识的整理总结,因为篇幅较长,MySQL基础知识余下部分发表在余下博客中 DML语言学习\插入数据\删除 ...

  5. hive sql 根据出生日期计算年龄(闰年同样准确)

    需求:根据用户出生日期,用hive sql计算用户年龄 由于闰年原因,直接用datediff计算当前与出生日期天数去除以365会造成年龄不准确,所以对数据进行处理来计算准确的年龄 select if( ...

  6. excel 通过身份证 计算 年龄、性别

    =IF(MOD(MID(D3,15,3),2),"男","女") =2022-MID(D3,7,4) 其中D3为身份证所在x.y 坐标

  7. SQL Server 出生日期计算年龄

    1. 粗略计算 datediff(year, BirthDay, getdate()) 2. 精确计算 floor((DateDiff(day, BirthDay, getdate()))/365.2 ...

  8. HQL怎么用身份证计算年龄

    floor(( UNIX_TIMESTAMP(current_date) -UNIX_TIMESTAMP(substr(card_no,7,8),'yyyyMMdd'))/(365*24*60*60) ...

  9. Excel根据出生日期和身份证使用公式计算年龄

    根据身份证计算年龄,并且出生月份大于当前月份,则年龄加1 =IF(MONTH(DATE(MID(D1,7,4),MID(D1,11,2),MID(D1,13,2))) < MONTH(NOW() ...

  10. 根据身份证号计算年龄、性别

    import java.text.SimpleDateFormat; import java.util.Date; //身份证处理 public class IDUtils { /** * 根据身份证 ...

最新文章

  1. redis - 基础
  2. BZOJ3771 Triple(FFT+容斥原理)
  3. Problem 25
  4. Spark之SparkSQL理论篇
  5. 蔚来Q3营收近百亿、毛利率20.3%,预计明年下半年推出2款新车
  6. [Python] Request module
  7. Cisco dynamips模拟器安装指南
  8. Ghost XP_sp3电脑装机终极版V9.6 【雪豹】
  9. 自制jlink ob stm32f072 版本。带串口
  10. 双光耦开关电源电路图_光耦817应用电路图汇总(PC817光电耦合器/开关电源/TL431)...
  11. Grafana dashboard 定时报表(Grafana-reporter)
  12. c/c++ 头文件(.h)、源文件(.cpp)书写及接口与实现分离实例
  13. AC/DC 电源适配器拆解
  14. 国产品牌积极布局,游戏手机会是行业增长新风口吗?
  15. 根据出生年月日算出实际的月龄
  16. 3D MAX眼睛贴图制作过程
  17. CP2101 usb转uart驱动
  18. 【MATLAB深度学习】10行代码实现图像分类
  19. 【matlab】:matlab中如何取整?
  20. UE4 高频 Fatal Error: exiting due to D3D device being lost解决方法:

热门文章

  1. 数字图像处理冈萨雷斯版学习(二)
  2. 如何给绘制好的CAD图纸设置密码?
  3. 计算机终端的串口并口指什么,串并口
  4. c语言词法分析器的实验报告,C语言词法分析器构造实验报告
  5. 内外网同时上怎么设置
  6. xposed框架android9.0,xposed仓库商店下载
  7. togaf简介(一)
  8. python商业爬虫学徒计划_(教程)下载:麻瓜编程Python商业爬虫学徒计划麻瓜编程的视频python办公自动化麻瓜...
  9. opencv2.4.9 + vc2012配置过程记录
  10. emu8086,汇编程序:屏幕输入字符串,设置寄存器的值