mybatis query sql statement period (using a MySQL function DATE_FORMAT)

Scene : a sql query there is a restriction is to limit the time period.

Here is my XML file in SQL

<select id="queryKpiByElems" resultMap="kpiEntity" parameterType="com.nari.platform.entity.KpiReqVo">
		SELECT * FROM monitor_statistics_latest
		WHERE `bc_id` = #{bcId}
		AND `kpi_name` = #{kpiName}
		AND `kpi_type` = #{kpiType}
		<if test="startTime !=null and startTime != ''">
			<![CDATA[   and DATE_FORMAT(created_at, '%Y-%m-%d')>=  DATE_FORMAT(#{startTime}, '%Y-%m-%d')   ]]>
		</if>
		<if test="endTime !=null and endTime != ''">
			<![CDATA[   and DATE_FORMAT(created_at, '%Y-%m-%d')<  DATE_FORMAT(#{endTime}, '%Y-%m-%d')   ]]>
		</if>
		ORDER BY `created_at` DESC
</select>

Here we use a MySQL function DATE_FORMAT ()

mysql of the DATE_FORMAT () function

Is defined: the DATE_FORMAT () function is used to display the date / time data in different formats.
Syntax: DATE_FORMAT(date,format)
DATE parameter is legitimate date. format output format predetermined date / time.
See detailed tutorial W3school link

Common format

format Example result
%Y-%m-%d 2020-02-12
%Y-%m-%d %H:%i:%s 22020-02-12 04:55:46
%b %d %Y %h:%i %p February 12, 2020 4:58 AM
Published 19 original articles · won praise 4 · Views 1565

Guess you like

Origin blog.csdn.net/DATANGguanjunhou/article/details/104269667