一、注意>=、<=、<> 等sql关系符号的使用
如下代码:
<select id="queryGoodsList" resultType="com.sanbang.vo.GoodsInfo" parameterType="java.util.Map">
select g.id,
g.name,
IFNULL((select s.salePrice from ezs_goods_audit_process s where s.goods_id = g.id and s.deleteStatus = '0'),0.00) as price,
g.inventory,
CONCAT("https://m.ezaisheng.com/",REPLACE(a.path,'\\','/'),"/",a.name) picturePath,
d1.name colorName,
ar.areaName areaName,
d2.name utilName,
g.area_id ,
(select IFNULL(SUM(gc.count),0) from ezs_orderform ef LEFT JOIN ezs_goodscart gc on ef.id = gc.of_id where ef.deleteStatus = '0' and gc.goods_id = g.id and (ef.order_status BETWEEN 50 AND 110 and ef.order_status <> 100)) as salecount
from ezs_goods g,ezs_accessory a,ezs_dict d1,ezs_area ar,ezs_dict d2
where g.goods_main_photo_id = a.id and g.color_id = d1.id and g.area_id = ar.id and g.util_id = d2.id AND g.`status`=2 and g.deleteStatus = '0'
<if test="sales != null and sales == 1">
ORDER BY salecount DESC
</if>
limit #{pageStart},#{pageSize}
</select>
注意其中:
and (ef.order_status >= 50 and ef.order_status <= 110 and ef.order_status <> 100)
在SQL中是没问你题的,在Mybaties中会报错。
其中可以使用 BETWEEN 50 AND 110 取代 ef.order_status >= 50 and ef.order_status <= 110
亦可以使用如下模式:
and (ef.order_status BETWEEN 50 AND 110 and ef.order_status <> 100)
用 >=取代 >=、<=取代<=、使用 <> 取代 <>,否者编译是过不去的。
注意 between and 包含边界值 区间 如:[2,4]
二、在sql片段里面对同一字段使用多个foreach标签
<select id="getPriceTrendcyNew" resultMap="PriceTrendcyNewResultMap">
select
IFNULL(AVG(a.price),0) currentPrice,
a.data_time dealDate,
gc.name className,
(select area.areaName from ezs_area area where area.id = a.region_id) areaName ,
a.region_id,
(
select IFNULL(AVG(p.price),0)
from ezs_price_trend p where p.status = '2' and p.goodClass2_id = a.goodClass2_id and date_add(DATE_FORMAT(p.data_time,'%Y-%m-%d'),interval 1 day) = DATE_FORMAT(a.data_time,'%Y-%m-%d')
<if test="areaIds != null and areaIds.size > 0 " >
and p.region_id
<foreach collection="areaIds" index="index" item="areaIds_1" open="in (" close=")" separator=",">
#{areaIds_1}
</foreach>
</if>
GROUP BY DATE_FORMAT(p.data_time,'%Y-%m-%d')
) prePrice
from ezs_price_trend a LEFT JOIN ezs_goods_class gc on a.goodClass2_id = gc.id
where a.status = '2'
<if test="kindId != null" >
and a.goodClass2_id = #{kindId}
</if>
<if test="areaIds != null and areaIds.size > 0 " >
and a.region_id
<foreach collection="areaIds" index="index" item="areaIds_2" open="in (" close=")" separator=",">
#{areaIds_2}
</foreach>
</if>
<!-- 筛选条件:一周、一月、一季度、一年 -->
<choose>
<when test="dateBetweenType != null and dateBetweenType == 'WEEK' ">
and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 WEEK)
</when>
<when test="dateBetweenType != null and dateBetweenType == 'MONTH' ">
and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH)
</when>
<when test="dateBetweenType != null and dateBetweenType == 'QUARTER' ">
and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 QUARTER)
</when>
<when test="dateBetweenType != null and dateBetweenType == 'YEAR' ">
and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 YEAR)
</when>
<otherwise>
<!-- and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH) -->
and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH)
</otherwise>
</choose>
GROUP BY DATE_FORMAT(a.data_time,'%Y-%m-%d') ORDER BY DATE_FORMAT(a.data_time,'%Y-%m-%d') desc <!-- LIMIT #{startPos},#{pageSize} -->
</select>
其中:
and p.region_id
<foreach collection="areaIds" index="index" item="areaIds_1" open="in (" close=")" separator=",">
#{areaIds_1}
</foreach>
和:
and a.region_id
<foreach collection="areaIds" index="index" item="areaIds_2" open="in (" close=")" separator=",">
#{areaIds_2}
</foreach>
第二段针对父查询的region_id进行筛选,第一段是针对子查询里面的region_id进行筛选,传入mapper中集合areaIds是一个,item需要区分开,例如:areaIds_2、areaIds_1。
注意里面两个foreach标签的使用!!!!