普通<= >=和between的sql查询方式区别与推荐

 

 

推荐SQL Server精准时间查询方式

复制代码
USE Test

/*插入或修改3条时间为以下极端情况的记录
UPDATE dbo.UserInfo SET AddTime = '2016-8-1 00:00:00' WHERE id=2
UPDATE dbo.UserInfo SET AddTime = '2016-8-1 23:59:59.005' WHERE id=3
UPDATE dbo.UserInfo SET AddTime = '2016-8-2 00:00:00' WHERE id=4
*/
--三条记录如下
SELECT * FROM dbo.UserInfo WITH(NOLOCK) WHERE id IN (2,3,4)
--BETWEEN对比的是两侧<=(小于等于)或>=(大于等于),查询出来的结果会略显不精准
SELECT * FROM dbo.UserInfo WITH(NOLOCK) WHERE AddTime BETWEEN '2016-8-1 00:00:00' AND  '2016-8-2 00:00:00'
--传统的大小于等于的方式,在59秒005毫秒的情况下是无法被查询出来的,就出现了数据错误的情况
SELECT * FROM dbo.UserInfo WITH(NOLOCK) WHERE '2016-8-1 00:00:00' <= AddTime AND AddTime <= '2016-8-1 23:59:59'
--对于时间要求较高的功能还是要使用两天之间0点的方式前面使用<=(小于等于)后面使用<(小于)第二天0点的方式
SELECT * FROM dbo.UserInfo WITH(NOLOCK) WHERE '2016-8-1 00:00:00' <= AddTime AND AddTime < '2016-8-2 00:00:00'
复制代码

转载于:https://www.cnblogs.com/firstdream/p/7273930.html

vo类 public class ArealRainfallVo implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "区域编码") private String code; @ApiModelProperty(value = "雨量总") private BigDecimal value; } Mapper类 public interface WeightBetweenObjectAndRainfallStationMapper extends BaseMapper<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> ListbySumList (List<PointRainfallSumVO> list); } mapper配置类 <mapper namespace="com.ndwp.jboot.bootstrap.mapper.WeightBetweenObjectAndRainfallStationMapper"> <select id="ListbySumList" resultType="com.ndwp.jboot.bootstrap.entity.vo.ArealRainfallVo"> SELECT code, SUM(CASE <foreach collection="list" item = "item"> WHEN rainfall_station_code = '${item.getCode()}' THEN weight * ${item.getValue()} </foreach> END ) AS value FROM bootstrap.weight_between_object_and_rainfall_station GROUP BY code; </select> </mapper> 服务类 public interface IWeightBetweenObjectAndRainfallStationService extends BaseService<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> listAreaRainfall(Date start, Date end); } 服务实现类 @Service public class WeightBetweenObjectAndRainfallStationServiceImpl extends BaseServiceImpl<WeightBetweenObjectAndRainfallStationMapper, WeightBetweenObjectAndRainfallStation> implements IWeightBetweenObjectAndRainfallStationService { @Autowired IDataHistoricRainfallService dataHistoricRainfallService; @Override public List<ArealRainfallVo> listAreaRainfall(Date start, Date end) { List<PointRainfallSumVO> pointRainfallSumVOS = dataHistoricRainfallService.listAllPointRainfallByTime(start, end); return baseMapper.ListbySumList(pointRainfallSumVOS); } } 控制台打印的sql语句能够正常运行,结合代码分析一下为何会返回四个空对象?
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值