日期:2014-05-17  浏览次数:20828 次

急!!在ibatis里怎么写union!
SQL code

SELECT  origin FROM game_user  WHERE plat_name='gamedp'  AND game_name='dc2' AND area_name='一服'
UNION
SELECT  origin FROM game_user  WHERE plat_name='gamedp'  AND game_name='dc2' AND area_name='二服'
UNION
SELECT  origin FROM game_user  WHERE plat_name='gamedp'  AND game_name='dc2' AND area_name='s1'



java里有个数组a[]={“一服”,“二服”,“s1”}

ibatis里的配置
XML code

<select id="queryOrigin"  parameterType="java.lang.String" resultType="com.aiyounet.gas.service.bean.GameUser">
 <foreach collection="array"  item="areaName"  open="(" separator="," close=")">
         SELECT  DISTINCT  origin AS origin,id AS id FROM game_user  WHERE plat_name=#{platName}  AND game_name=#{gameName}  AND area_name=#{areaName}
         <if test=" array.length >1 ">  
           UNION
        </if> 
</foreach>
</select>
这样写可以不?




------解决方案--------------------
同一个表里,用area_name in ('一服','二服','s1')就可以了
不同表才有union
------解决方案--------------------
<select id="queryOrigin" parameterType="java.lang.String" resultType="com.aiyounet.gas.service.bean.GameUser">
SELECT DISTINCT origin AS origin,id AS id 
FROM game_user 
WHERE plat_name=#{platName}
AND game_name=#{gameName} 
 <if test="areaName!=null ">
 AND 
area_name in 
 <foreach collection="array" item="areaName" open="(" separator="," close=")">
</foreach>
 </if> 
</select>