您的位置:首页 > 数据库

对数据库中date类型字段进行条件查询以及sql中or和and的执行顺序

2019-03-21 09:11 399 查看

1、问题:

1、对数据库中date类型字段进行条件查询
2、sql中or和and的执行顺序
1、对数据库中date类型字段进行条件查询
<if test="forecastTime != null and forecastTime != ''">
and DATE_FORMAT(forecast_time,'%Y-%m-%d') = #{forecastTime,jdbcType=VARCHAR}
</if>

DATE_FORMAT函数在查询的时候把date类型的字段转换为字符串类型,相当于java中的SimpleDateFormat这个类,
‘%Y-%m-%d’ 这个是转换模板,forecastTime则为前端传过来的String 类型的日期
详情见 https://www.cnblogs.com/awzf/p/9835156.html

2、sql中or和and的执行顺序
select <include refid="Base_Column_List"/>
from water_forecast_record
where (is_pass = 1 or is_pass = 2)//这里加不加括号是两种查询情况

<if test="forecastTime != null and forecastTime != ''">
and DATE_FORMAT(forecast_time,'%Y-%m-%d') = #{forecastTime,jdbcType=VARCHAR}
</if>
<if test="needMeeting != null">
and need_meeting = #{needMeeting,jdbcType=INTEGER}
</if>
<if test="reportPeople != null and reportPeople != ''">
and report_people like concat("%",#{reportPeople,jdbcType=VARCHAR},"%")
</if>
<if test="checkPeople != null and checkPeople != ''">
and check_people like concat("%",#{checkPeople,jdbcType=VARCHAR},"%")
</if>
</select>
关系型运算符优先级高到低为:NOT >AND >OR
where (is_pass = 1 or is_pass = 2) 这里如果不加()or是会最后执行的

参考文章:https://blog.csdn.net/bingguang1993/article/details/79657256

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐