废话不多说直接上例子:
<select id="selectBuildingMsgByTimeFromBeginToEndAndAreaName" parameterType="java.util.Map" resultType="com.pojo.BuildingPojo">
SELECT b.props_and_uses_name as propsAndUsesName,
COUNT(*) as constructionNumbers,
COUNT(b.building_area) as constructionTotalArea
FROM tb_construction_info c
LEFT JOIN tb_building_info b
ON c.cid = b.cid
<where>
<if test="beginTime != null or beginTime != '' and endTime!=null or endTime != ''">
c.completion_date between #{beginTime,jdbcType=TIMESTAMP} and
#{endTime,jdbcType=TIMESTAMP}
</if>
<if test="beginTime != null or beginTime != '' and beginTime==null or endTime != ''">
AND c.completion_date > #{beginTime,jdbcType=TIMESTAMP}
</if>
<if test="beginTime == null or beginTime == '' and endTime!=null or endTime!=''">
AND c.completion_date < #{endTime,jdbcType=TIMESTAMP}
</if>
<if test="areaName != null and areaName != ''">
AND c.area_name = #{areaName,jdbcType=VARCHAR}
</if>
</where>
GROUP BY b.props_and_uses_name
ORDER BY c.completion_date DESC
</select>
注意点:where 条件一定要放到group by 语句上面否则会报错。
ps:追加一下什么是DESC 什么是ASC
DESC: 降序 从大到小,从高到底
ASC:升序排序,从 小到大,从底到高