欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库函数判断本周,本年,本月,本天,本季度

程序员文章站 2022-06-15 22:45:21
...
      //自定义:本周、本月、本季度、本年,本天查询条件
        public Criteria andTimeSearch(String time){
            //时间范围
            if(StringUtils.isNotEmpty(time)){
                if(time.equals("1")){
                    StringBuffer sb = new StringBuffer("YEARWEEK(date_format(create_date,'%Y-%m-%d')) = YEARWEEK(now())");
                    addCriterion(sb.toString()); 
                }
                if(time.equals("2")){
                    StringBuffer sb = new StringBuffer("DATE_FORMAT( create_date, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
                    addCriterion(sb.toString());
                }
                if(time.equals("3")){
                    StringBuffer sb = new StringBuffer("QUARTER(create_date)=QUARTER(now())");
                    addCriterion(sb.toString());
                }
                if(time.equals("4")){
                    StringBuffer sb = new StringBuffer("YEAR(create_date)=YEAR(NOW())");
                    addCriterion(sb.toString());
                }
                if(time.equals("5")){
                    StringBuffer sb = new StringBuffer("DATE_FORMAT(create_date,'%Y%m%d') = DATE_FORMAT(now(),'%Y%m%d')");
                    addCriterion(sb.toString());
                }
            }
            return this;  
        }
相关标签: 后端