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

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

你可能感兴趣的:(java)