mybatis同一个参数在同一条sql中多次使用如何处理?或者说在父查询和子查询中使用同一个where条件怎么办
mybatis同一个参数在同一条sql中多次使用如何处理?或者说在父查询和子查询中使用同一个where条件如何处理?传不进去值。SELECTaa.idASstationI...
mybatis同一个参数在同一条sql中多次使用如何处理?或者说在父查询和子查询中使用同一个where条件如何处理?传不进去值。
SELECT aa.id AS stationId, bb. NAME AS area, aa. NAME AS stationName, aa.address ,
aa.create_time AS createTime, cc.plugCount, format(cc.consumption, 2) AS consumption,
cc.plugUsedTimes AS usedTimes, cc.stationUsedDays AS usedDays, format(cc.realMoney, 2) AS realTurnover,
format(cc.virtualMoney,2) as virtualTurnover,
format( ( cc.realMoney - cc.consumption * IF ( aa.common2 IS NULL or aa.common2='',0.8,aa.common2) ) * 100 / cc.realMoney,
2 ) AS earnPercentage,format( cc.usedhour / (cc.stationUsedDays * 0.24 * cc.plugCount),2) AS usedPercentage
FROM ikeawell_charge_station aa
LEFT JOIN ikeawell_charge_area bb ON bb.id = aa.aid
LEFT JOIN ( SELECT a.stationId, a.plug_count as plugCount,sum(a.consumption) AS consumption ,
sum(a.plug_used_times) AS plugUsedTimes,sum(a.virtual_money) AS virtualMoney,
sum(a.real_money) as realMoney, (datediff(max(a.date), min(a.date)) + 1 ) AS stationUsedDays,
sum(a.usedhour) as usedhour FROM ikeawell_charge_statistics_station a ]]>
<where>
1=1
<if test="stationId=null and stationId!=''">
and a.stationId =${stationId}
</if>
<if test="startTime!=null and startTime !=''">
<![CDATA[ and a.create_time >= '${startTime}' ]]>
</if>
<if test="endTime!=null and endTime !=''">
<![CDATA[ and a.create_time <= '${endTime}' ]]>
</if>
</where>
<![CDATA[ group by a.stationId )cc ON aa.id = cc.stationId ]]>
<where>
1=1
<if test="area!=null and area !=''">
and bb.id =${area}
</if>
<if test="stationName!=null and stationName !=''">
and aa.name like '%${stationName}%'
</if>
<if test="stationId!=null and stationId !=''">
and aa.id =${stationId}
</if>
<if test="createTime!=null and createTime !=''">
<![CDATA[ and aa.create_time >= '${createTime}' ]]>
</if>
<if test="usedPercentage!=null and usedPercentage !=''">
and cc.usedhour/ (cc.stationUsedDays * 0.24 *cc.plugCount) >=${usedPercentage}
</if>
<if test="earnPercentage!=null and earnPercentage !=''">
<![CDATA[ and (cc.realMoney-(cc.consumption)*IF(aa.common2 is null or aa.common2 ='',0.8,aa.common2))*100/( cc.realMoney) >=${earnPercentage} ]]>
</if>
</where>
</select> 展开
SELECT aa.id AS stationId, bb. NAME AS area, aa. NAME AS stationName, aa.address ,
aa.create_time AS createTime, cc.plugCount, format(cc.consumption, 2) AS consumption,
cc.plugUsedTimes AS usedTimes, cc.stationUsedDays AS usedDays, format(cc.realMoney, 2) AS realTurnover,
format(cc.virtualMoney,2) as virtualTurnover,
format( ( cc.realMoney - cc.consumption * IF ( aa.common2 IS NULL or aa.common2='',0.8,aa.common2) ) * 100 / cc.realMoney,
2 ) AS earnPercentage,format( cc.usedhour / (cc.stationUsedDays * 0.24 * cc.plugCount),2) AS usedPercentage
FROM ikeawell_charge_station aa
LEFT JOIN ikeawell_charge_area bb ON bb.id = aa.aid
LEFT JOIN ( SELECT a.stationId, a.plug_count as plugCount,sum(a.consumption) AS consumption ,
sum(a.plug_used_times) AS plugUsedTimes,sum(a.virtual_money) AS virtualMoney,
sum(a.real_money) as realMoney, (datediff(max(a.date), min(a.date)) + 1 ) AS stationUsedDays,
sum(a.usedhour) as usedhour FROM ikeawell_charge_statistics_station a ]]>
<where>
1=1
<if test="stationId=null and stationId!=''">
and a.stationId =${stationId}
</if>
<if test="startTime!=null and startTime !=''">
<![CDATA[ and a.create_time >= '${startTime}' ]]>
</if>
<if test="endTime!=null and endTime !=''">
<![CDATA[ and a.create_time <= '${endTime}' ]]>
</if>
</where>
<![CDATA[ group by a.stationId )cc ON aa.id = cc.stationId ]]>
<where>
1=1
<if test="area!=null and area !=''">
and bb.id =${area}
</if>
<if test="stationName!=null and stationName !=''">
and aa.name like '%${stationName}%'
</if>
<if test="stationId!=null and stationId !=''">
and aa.id =${stationId}
</if>
<if test="createTime!=null and createTime !=''">
<![CDATA[ and aa.create_time >= '${createTime}' ]]>
</if>
<if test="usedPercentage!=null and usedPercentage !=''">
and cc.usedhour/ (cc.stationUsedDays * 0.24 *cc.plugCount) >=${usedPercentage}
</if>
<if test="earnPercentage!=null and earnPercentage !=''">
<![CDATA[ and (cc.realMoney-(cc.consumption)*IF(aa.common2 is null or aa.common2 ='',0.8,aa.common2))*100/( cc.realMoney) >=${earnPercentage} ]]>
</if>
</where>
</select> 展开
1个回答
展开全部
最简单的直接设置resultType=“map”,得到的就是一个List<Map<String,Object>>
每一个Map的key就是查询结果的列名,也就是表的字段名。当然sql语句中最好as一下
复杂一点可以自定义一个resultMap标签
<resultMap id="result" type="java.util.Map">
<result property="c1" column="column1"/>
<result property="c2" column="column2"/>
</resultMap>
其中result可以自定义,select标签中的resultMap="result",和上面的resultMap标签的id对应
property设置的是自定义的字段名称,也就是结果集中每一个Map的key
column设置查询结果的字段名称
然后通过
for(List<Map<String,Object> map:list){
map.get("c1");//获取c1对应的字段的值
。。。
}
每一个Map的key就是查询结果的列名,也就是表的字段名。当然sql语句中最好as一下
复杂一点可以自定义一个resultMap标签
<resultMap id="result" type="java.util.Map">
<result property="c1" column="column1"/>
<result property="c2" column="column2"/>
</resultMap>
其中result可以自定义,select标签中的resultMap="result",和上面的resultMap标签的id对应
property设置的是自定义的字段名称,也就是结果集中每一个Map的key
column设置查询结果的字段名称
然后通过
for(List<Map<String,Object> map:list){
map.get("c1");//获取c1对应的字段的值
。。。
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询