mysql查询,根据时间查询:几天前、几天内的数据

示例中 mysql字段类型为 datetime

使用到mysql函数 DATE_SUB() 和 now()

 

示例1:

    <!--
    更新5天前的订单 审核中-6 改成 审核失败-81
    create_time <= DATE_SUB(now(), INTERVAL 5 DAY) 5天前,精确到秒
    -->
    <update id="updateStatsByTime"  parameterType="java.lang.Integer" >
      UPDATE user_partya_order  SET stat = 81
        WHERE
            id IN(select id from  (
                SELECT
                    id
                FROM
                    user_partya_order
                WHERE
                    loan_id = #{id}
                AND create_time &lt;= DATE_SUB(now(), INTERVAL 5 DAY)
                and  stat = 6
            ) AS a )
    </update>

 

示例2:

    <!--
    查询用户5天内的订单
    create_time > DATE_SUB(now(), INTERVAL 5 DAY) 5天内,精确到秒
    -->
   <select id="findRemoveOrder" parameterType="java.util.Map"  resultMap="mapUserPartyaOrderDTO">
         SELECT id,create_time,order_no,loan_id, stat,user_id,borrower_id,loan_app_id,bank_no,is_repeat_loan
                FROM user_partya_order
                WHERE 1=1 AND stat='6'
                AND create_time &gt; DATE_SUB(now(), INTERVAL 5 DAY)
                AND loan_id =#{loanId}
                AND user_id=#{userId} ORDER BY create_time DESC  LIMIT 1
   </select>

 

核心标红!

 

posted @ 2020-04-02 15:21  007少侠  阅读(2075)  评论(0编辑  收藏
友情链接:电影呗
站长统计: