复杂的sql语句 + having

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间的值为dao层接口的权限定名 -->
<mapper namespace="com.bawei.dao.GdpDao" >

<select id="findAll" resultMap="maps">
select g.*,p.name as name,p.pnum as pnum,(g.industryIncome-g.industryInvest) as industryProfits,
((g.industryIncome-g.industryInvest)/g.industryIncome*100) as industryProfitsMargin,
(g.agIncome-g.agInvest) as agProfits,
((g.agIncome-g.agInvest)/g.agIncome*100) as agProfitsMargin,
(g.industryIncome/(g.industryIncome+g.agIncome)*100) as industryBiZhong
from tb_gdp g LEFT JOIN tb_province p ON g.provinceId=p.id
<where>
<if test="provinceId!=null and provinceId!=0 ">
and provinceId=#{provinceId}
</if>
<if test="pnumMin!=null ">
and pnum &gt;= #{pnumMin}
</if>
<if test="pnumMax!=null ">
and pnum &lt;= #{pnumMax}
</if>
<if test="profitsMin!=null ">
and ((g.agIncome+g.industryIncome-g.agInvest-g.industryInvest)/(g.agIncome+g.industryIncome)*100) &gt;= #{profitsMin}
</if>
<if test="profitsMax!=null ">
and ((g.agIncome+g.industryIncome-g.agInvest-g.industryInvest)/(g.agIncome+g.industryIncome)*100) &gt;= #{profitsMax}
</if>
<if test="industryInvestMin!=null ">
and g.industryInvest &gt;= #{industryInvestMin}
</if>
<if test="industryInvestMax!=null ">
and g.industryInvest &lt;= #{industryInvestMax}
</if>
<if test="agIncomeMin!=null ">
and g.agIncome &gt;= #{agIncomeMin}
</if>
<if test="agIncomeMax!=null ">
and g.agIncome &lt;= #{agIncomeMax}
</if>
<if test="monthMin!=null ">
and g.month &gt;= #{monthMin}
</if>
<if test="monthMax!=null ">
and g.month &lt;= #{monthMax}
</if>
</where>
GROUP BY g.id
<trim prefix="having" prefixOverrides="and" >
<if test="yearMin!=null ">
and SUM(g.agIncome+g.industryIncome) &gt;= #{yearMin}
</if>
<if test="yearMax!=null ">
and SUM(g.agIncome+g.industryIncome) &lt;= #{yearMax}
</if>
</trim>
</select>

<resultMap type="Gdp" id="maps">
<id property="id" column="id" />
<result property="provinceId" column="provinceId" />
<result property="month" column="month" />
<result property="industryInvest" column="industryInvest" />
<result property="industryIncome" column="industryIncome" />
<result property="eConsumer" column="eConsumer" />
<result property="agInvest" column="agInvest" />
<result property="agIncome" column="agIncome" />

<result property="industryProfits" column="industryProfits" />
<result property="industryProfitsMargin" column="industryProfitsMargin" />
<result property="agProfits" column="agProfits" />
<result property="agProfitsMargin" column="agProfitsMargin" />
<result property="industryBiZhong" column="industryBiZhong" />

<!-- <association property="pro" javaType="Province">
<id property="pid" column="pid" />
<result property="name" column="name" />
<result property="pnum" column="pnum" />
</association> -->
</resultMap>




</mapper>

posted @ 2020-05-13 09:43  兆龙  阅读(205)  评论(0)    收藏  举报