sql时间判断,在某一端时间内的数据

     SELECT
        p.container_name AS containerName,
        cargo_name AS cargoName,
        cargo_maintain_time_now AS cargoExpireTime
        FROM
        t_cargo t
        LEFT JOIN (
        SELECT t1.id, t1.container_name AS container_name FROM t_container t1
        UNION ALL
        SELECT t2.id, t2.shelf_address AS container_name FROM t_shelf t2
        ) p ON t.location_id = p.id
        LEFT JOIN t_store d ON t.location_id = d.id
        WHERE
        t.state = 1
        and t.storehouse_num = #{storehouseNum}
        <if test='timeType=="1"'>
            AND (DATEDIFF(now(), cargo_maintain_time_now) >=-7 and DATEDIFF(now(), cargo_maintain_time_now) &lt;=7 )
        </if>
        <if test='timeType=="2"'>
            AND (DATEDIFF(now(), cargo_maintain_time_now) >=-30 and DATEDIFF(now(), cargo_maintain_time_now) &lt;=30 )
        </if>
        ORDER BY cargo_maintain_time_now ASC
      SELECT
        p.container_name AS containerName,
        cargo_name AS cargoName,
        cargo_maintain_time_now AS cargoExpireTime
        FROM
        t_cargo t
        LEFT JOIN (
        SELECT t1.id, t1.container_name AS container_name FROM t_container t1
        UNION ALL
        SELECT t2.id, t2.shelf_address AS container_name FROM t_shelf t2
        ) p ON t.location_id = p.id
        LEFT JOIN t_store d ON t.location_id = d.id
        WHERE
        t.state = 1
        and t.storehouse_num =1599963047676340007
        AND (DATEDIFF(now(), cargo_maintain_time_now) >=-30 and DATEDIFF(now(), cargo_maintain_time_now) <=30 )
      
			
		

表示

DATEDIFF函数,用啦之后 那现在的时间写前面,要判断的时间写后面,然后写一个范围就行啦
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值