zoukankan      html  css  js  c++  java
  • mybatis查询时间段sql语句

    转载自:http://blog.csdn.net/zl544434558/article/details/24428307?utm_source=tuicool&utm_medium=referral

    页面表现形式:

    查询Ro

    页面传过来的就是字符串

    public class QueryRo {
    
        private String beginTime;
    
        private String endTime;
    
        public String getBeginTime() {
            return beginTime;
        }
    
        public void setBeginTime(String beginTime) {
            this.beginTime = beginTime;
        }
    
        public String getEndTime() {
            return endTime;
        }
    
        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }
    }

    在项目中查询时间段的sql语句(时间类型为datetime或date)(数据库中的时间类型):

    <if test="beginTime!=null and beginTime!=''">
        <![CDATA[   and DATE_FORMAT(tr.add_time, '%Y-%m-%d')>=  DATE_FORMAT(#{beginTime}, '%Y-%m-%d')   ]]>
    </if>
    <if test="endTime!=null and endTime!=''">
        <![CDATA[  and DATE_FORMAT(tr.add_time, '%Y-%m-%d') <= DATE_FORMAT(#{endTime}, '%Y-%m-%d')    ]]>
    </if>

    在项目中查询时间段的sql语句(时间类型为varchar)(数据库中的时间类型)

    <if test="beginTime!=null and beginTime!=''">
      AND tm.add_time&gt;=#{beginTime} 
    </if> <if test="endTime!=null and endTime!=''">   AND tm.add_time &lt;=#{endTime}
    </if>

    &lt;小于号  <      &gt; 大于号>

    附:

    MySQL DATE_FORMAT() 函数

    定义和用法

    DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。

    语法

    DATE_FORMAT(date,format)

    date 参数是合法的日期。format 规定日期/时间的输出格式。

    可以使用的格式有:

    格式描述
    %a 缩写星期名
    %b 缩写月名
    %c 月,数值
    %D 带有英文前缀的月中的天
    %d 月的天,数值(00-31)
    %e 月的天,数值(0-31)
    %f 微秒
    %H 小时 (00-23)
    %h 小时 (01-12)
    %I 小时 (01-12)
    %i 分钟,数值(00-59)
    %j 年的天 (001-366)
    %k 小时 (0-23)
    %l 小时 (1-12)
    %M 月名
    %m 月,数值(00-12)
    %p AM 或 PM
    %r 时间,12-小时(hh:mm:ss AM 或 PM)
    %S 秒(00-59)
    %s 秒(00-59)
    %T 时间, 24-小时 (hh:mm:ss)
    %U 周 (00-53) 星期日是一周的第一天
    %u 周 (00-53) 星期一是一周的第一天
    %V 周 (01-53) 星期日是一周的第一天,与 %X 使用
    %v 周 (01-53) 星期一是一周的第一天,与 %x 使用
    %W 星期名
    %w 周的天 (0=星期日, 6=星期六)
    %X 年,其中的星期日是周的第一天,4 位,与 %V 使用
    %x 年,其中的星期一是周的第一天,4 位,与 %v 使用
    %Y 年,4 位
    %y 年,2 位

    实例

    下面的脚本使用 DATE_FORMAT() 函数来显示不同的格式。我们使用 NOW() 来获得当前的日期/时间:

    DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
    DATE_FORMAT(NOW(),'%m-%d-%Y')
    DATE_FORMAT(NOW(),'%d %b %y')
    DATE_FORMAT(NOW(),'%d %b %Y %T:%f')

    结果类似:

    Dec 29 2008 11:45 PM
    12-29-2008
    29 Dec 08
    29 Dec 2008 16:25:46.635
  • 相关阅读:
    map-count
    map-count
    map-constructors
    map-constructors
    multiset-find
    multiset-find
    multiset-insert
    C++ string详解
    treap(树堆)
    程序设计语言的变革
  • 原文地址:https://www.cnblogs.com/winner-0715/p/5912157.html
Copyright © 2011-2022 走看看