如果数据库表中有涉及到金额的部分,很多时候都需要查询每个月最后一天数据。该语句是用来查询每个月最后一天的。
sql语句的写法:
<select id = "selectLastDayEachMonth" parameterType = "hashmap" resultMap = "BaseResultMap">
select * from table(table写自己的表名称)
where date_format
(create_time,'%Y-%m-%d') in
(LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-01'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-02'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-03'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-04'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-05'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-06'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-07'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-08'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-09'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-10'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-11'),'%Y-%m-%d')),
LAST_DAY ( STR_TO_DATE (concat (#{current_time,jdbcType=VARCHAR},'-12'),'%Y-%m-%d')))
其中有两个地方需要注意,第一个地方是date_format括号里面的create_time,这个是你表中存放时间的字段,如果你的表中的存放时间的字段是time,就写time
第二个需要注意的地方,current_time是你要查询的每个月的所在年份。比如今年是2017年,那么current_time就是2017.
对应的java代码的封装是这样的:("current_time",'2017"),其他地方都是写死的,不需要做任何改变。