一條sql語句兩次foreach迭代問題
阿新 • • 發佈:2019-02-12
SELECT date_format(a.DataTime,'%H:%i') AS dateTime,SUM(a.nowpower) AS sum FROM
(select drt.DataTime,drt.Pac AS nowpower FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sn" index="index" collection="sn" open="(" separator="," close=")">
#{sn}
</foreach>
</if>
) AS d
INNER JOIN dataforreadtime drt ON d.DeviceSN=drt.DeviceSN WHERE DATE_FORMAT(drt.DataTime,"%Y-%m-%d")=#{dateTime}
UNION ALL
SELECT sdt.DataTime,getStoreDevicePower(sdt.PV1Power,sdt.PV2Power,sdt.PV3Power,sdt.PV4Power,sdt.PV5Power,sdt.PV6Power) AS nowpower
FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sn" index="index" collection="sn" open="(" separator="," close=")">
#{sn}
</foreach>
</if>
) AS d
INNER JOIN store_dataforrealtime sdt ON d.DeviceSN=sdt.DeviceSN WHERE DATE_FORMAT(sdt.DataTime,"%Y-%m-%d")=#{dateTime}
" open="(" separator="," close=")">
#{sn}
</foreach>
</if>
) AS d
INNER JOIN dataforreadtime drt ON d.DeviceSN=drt.DeviceSN WHERE DATE_FORMAT(drt.DataTime,"%Y-%m-%d")=#{dateTime}
UNION ALL
SELECT sdt.DataTime,getStoreDevicePower(sdt.PV1Power,sdt.PV2Power,sdt.PV3Power,sdt.PV4Power,sdt.PV5Power,sdt.PV6Power) AS nowpower
FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sns" index="index" collection="sns " open="(" separator="," close=")">
#{sns}
</foreach>
</if>
) AS d
INNER JOIN store_dataforrealtime sdt ON d.DeviceSN=sdt.DeviceSN WHERE DATE_FORMAT(sdt.DataTime,"%Y-%m-%d")=#{dateTime}
)AS a GROUP BY a.DataTime
(select drt.DataTime,drt.Pac AS nowpower FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sn" index="index" collection="sn" open="(" separator="," close=")">
#{sn}
</foreach>
</if>
) AS d
INNER JOIN dataforreadtime drt ON d.DeviceSN=drt.DeviceSN WHERE DATE_FORMAT(drt.DataTime,"%Y-%m-%d")=#{dateTime}
UNION ALL
SELECT sdt.DataTime,getStoreDevicePower(sdt.PV1Power,sdt.PV2Power,sdt.PV3Power,sdt.PV4Power,sdt.PV5Power,sdt.PV6Power) AS nowpower
FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sn" index="index" collection="sn" open="(" separator="," close=")">
#{sn}
</foreach>
</if>
) AS d
INNER JOIN store_dataforrealtime sdt ON d.DeviceSN=sdt.DeviceSN WHERE DATE_FORMAT(sdt.DataTime,"%Y-%m-%d")=#{dateTime}
)AS a GROUP BY a.DataTime
傳過來同一個sn陣列查詢報錯,解決方案:把兩個foreach名不一樣,兩個相同的陣列
SELECT date_format(a.DataTime,'%H:%i') AS dateTime,SUM(a.nowpower) AS sum FROM
(select drt.DataTime,drt.Pac AS nowpower FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sn" index="index" collection="sn
#{sn}
</foreach>
</if>
) AS d
INNER JOIN dataforreadtime drt ON d.DeviceSN=drt.DeviceSN WHERE DATE_FORMAT(drt.DataTime,"%Y-%m-%d")=#{dateTime}
UNION ALL
SELECT sdt.DataTime,getStoreDevicePower(sdt.PV1Power,sdt.PV2Power,sdt.PV3Power,sdt.PV4Power,sdt.PV5Power,sdt.PV6Power) AS nowpower
FROM (SELECT pd.DeviceSN FROM plant p,plantdevice pd WHERE p.PlantUID=pd.PlantUid AND p.PlantUID=#{plantUid}
<if test="sn != null and sn != ''">
and pd.DeviceSN in
<foreach item="sns" index="index" collection="sns
#{sns}
</foreach>
</if>
) AS d
INNER JOIN store_dataforrealtime sdt ON d.DeviceSN=sdt.DeviceSN WHERE DATE_FORMAT(sdt.DataTime,"%Y-%m-%d")=#{dateTime}
)AS a GROUP BY a.DataTime