当前位置: 代码迷 >> Java Web开发 >> 急在ibatis里如何写union
  详细解决方案

急在ibatis里如何写union

热度:3257   发布时间:2013-02-25 21:12:17.0
急!!在ibatis里怎么写union!
SQL code
SELECT  origin FROM game_user  WHERE plat_name='gamedp'  AND game_name='dc2' AND area_name='一服'UNIONSELECT  origin FROM game_user  WHERE plat_name='gamedp'  AND game_name='dc2' AND area_name='二服'UNIONSELECT  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>
  相关解决方案