Mybatis根据时间段查询

如果你要严格要求是某一年的,那可以这样

查询一天:

select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate(); 

查询一周:

select * from table  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);


查询一个月:

select * from table  where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(column_time);

查询某一个月

select * from exam where date_format(starttime,'%Y-%m')='2017-10' 
select * from exam where date_format(starttime,'%Y-%m')=date_format('2017-10-05','%Y-%m')
查询某一天
select * from exam where date_format(starttime,'%Y-%m-%d')=date_format('2017-10-05','%Y-%m-%d')
查询一年:

select * from table  where DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(column_time);
两个字段
方法一 
SELECT
    topic_id as topicId,id,
    count( topic_id ) AS topicSum
    FROM
    topic_memo
    WHERE
    STATUS = 1
    AND memo_type = 1
    AND actual_time IS NULL
    AND user_id = 4
    AND '2022-11-19' BETWEEN begin_time and end_time
    GROUP BY
    topic_id
		方法二
	SELECT
    topic_id as topicId,id,
    count( topic_id ) AS topicSum
    FROM
    topic_memo
    WHERE
    STATUS = 1
    AND memo_type = 1
    AND actual_time IS NULL
    AND user_id = 4
    AND begin_time <='2022-11-11' and '2022-11-11' <=end_time
    GROUP BY
    topic_id

判断是否满足某个条件

 <select id="selectByTeamId" resultType="java.lang.Integer">
        select count(*) from  jf_consultation_record as a where
            doctorId=#{id} and cstate=0
          <if test="typeId!=null and typeId=='1'.toString()">
            and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <![CDATA[ <= ]]> date(ctime)
          </if>
          <if test="typeId!=null and typeId=='2'.toString()">
            and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <![CDATA[ <= ]]> date(ctime)
          </if>
          <if test="typeId!=null and typeId=='3'.toString()">
            and DATE_SUB(CURDATE(), INTERVAL INTERVAL 3 MONTH) <![CDATA[ <= ]]> date(ctime)
          </if>
  </select>
-- 时间段查询
<!--      <if test="taskTime!=null">-->
<!--       AND date_format(a.task_start_time,'%Y-%m-%d')= date_format(#{taskTime},'%Y-%m-%d')-->
<!--      </if>-->
      <if test="taskTime != null">
         and date_format(a.task_start_time, '%Y-%m-%d') &gt;= date_format(#{taskTime},'%Y-%m-%d')
      </if>
      <if test="taskEndTime != null">
         and date_format(a.task_start_time, '%Y-%m-%d') &lt;= date_format(#{taskEndTime},'%Y-%m-%d')
      </if>
      
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值