mysql区间范围查询问题

一,日期区间查询,表里有一个时间字段

最常见的就是某时间段查询,比如xxxx时间---xxxx时间有多少条数据。例如数据库里的字段是 income_period,

该字段类型可以是字符串(varchar),可以是时间类型(datetime,date),mysql都支持区间查询

下面就以字符串类型为例 

1 数据库表里字段
2 income_period  varchar (10)  NULL 
3 
4 对应的mapper
5 <result column="income_period" property="incomePeriod" jdbcType="VARCHAR" />

对应的参数时间字段是 dateFrom,dateTo ,

具体的sql语句是

 1 <select id="queryIncomeList" resultMap="Income" parameterType="java.util.Map">
 2     select * FROM income m    WHERE m.del_flg="N"
 3     
 4     <if test="dateFrom != null and dateFrom != ''">
 5         AND m.`income_period` >= #{dateFrom,jdbcType=VARCHAR}
 6     </if>
 7     <if test="dateTo != null and dateTo != ''">
 8         AND #{dateTo,jdbcType=VARCHAR}>=m.`income_period`
 9     </if>
10     
11     order by m.income_period
12 
13 </select>

二,数字区间,表里有两个金额字段,形成区间

具体金额落在哪个区间,

1 数据库表里的字段
2 min_tax decimal(16,2) NULL最小值
3 max_tax decimal(16,2) NULL最大值
4 
5 对应的mapper
6 <result column="min_tax" jdbcType="DECIMAL" property="minTax" />
7 <result column="max_tax" jdbcType="DECIMAL" property="maxTax" />

对应的参数金额字段saveMoney

具体的sql语句

1 <select id="queryPoster" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
2     select * from iit_share_poster
3     WHERE  #{saveMoney,jdbcType=DECIMAL}>= min_tax
4     
5     AND max_tax >=#{saveMoney,jdbcType=DECIMAL}
6 
7 </select>

mysql会根据dao层传入的参数类型要与mapper中的参数对应。

 

posted @ 2019-02-15 14:49  java白丁  阅读(17482)  评论(0编辑  收藏  举报