mybatis分页多条件查询指定时间段数据的sql语句
2017-10-24 19:07
851 查看
本案例适用于mysql数据库,在数据库中时间数据类型为datetime。
使用以下sql语句将不会报错,但也得不到想要的结果;
<select id="getGuidesList" parameterType="map" resultMap="GuideResultMap" flushCache="true">
SELECT
a.guide_id,a.pat_id,a.direct_reason,a.sport_direct,a.food_direct,a.drug_suggest,a.guide_time,a.memo,b.name,c.doctorName
from (pc_healthguide a LEFT JOIN pc_patient b on a.pat_id=b.pat_id) LEFT JOIN pc_doctor c on a.doctor_id=c.doc_id
<where>
<if test="doctorId!=null and doctorId !='0'">
a.doctor_id=#{doctorId}
</if>
<if test="name != null and name!= ''">
and name =#{name}
</if>
<if test="directReason!=null and directReason !=''">
and direct_reason=#{directReason}
</if>
<if test="guide_time!=null and guide_time!=''">
and guide_timebetween #{beginTime} and #{endTime}
</if>
</where>
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>
项目运行后,select的sql语句where并没有拼接and createTime between #{beginTime} and #{endTime}这一串,正确用法这样才对:
<if test="directReason!=null and directReason !=''">
and direct_reason=#{directReason}
</if>
<if test="beginDate!=null and beginDate!=''">
<![CDATA[ and DATE_FORMAT(guide_time, '%Y-%m-%d')>= DATE_FORMAT(#{beginDate}, '%Y-%m-%d') ]]>
</if>
<if test="endDate!=null and endDate!=''">
<![CDATA[ and DATE_FORMAT(guide_time, '%Y-%m-%d') <= DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>
</if>
其中:beginDate和endDate实体类的属性,guide_time为数据库中的字段名,不要搞混淆了。
相关文章推荐
- 数据库SQL语句查询指定时间段内的数据
- Mybatis自己写sql语句分页查询数据
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- oracel sql语句:查询时间段中开始时间与结束时间相同的数据
- ASPNETCOREAPI 跨域处理 SQL 语句拼接 多条件分页查询 ASPNET CORE 核心 通过依赖注入(注入服务)
- mybatis查询时间段sql语句
- sql 多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘
- mybatis执行查询语句查询不到结果,控制台输出sql语句复制到数据库中能查到数据
- Oracle学习之路(一):oracle简介+基本sql语句+条件查询+排序数据理论与案例
- mybatis查询时间段sql语句
- 在同一个sql语句中写不同条件的count数量,和查询半年的数据
- mybatis执行查询语句查询不到结果,控制台输出sql语句复制到数据库中能查到数据
- MyBatis中动态SQL语句完成多条件查询
- 多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘
- sql查询语句(全查或查指定数据)
- mybatis分页条件查询动态sql
- sql语句查询某字段中包含指定字符串的数据列表,完全匹配字段中逗号中间的字符串数据
- Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
- sql语句,查询时间段的数据
- 多条件查询 分组 分页的sql语句