您的位置:首页 > 数据库 > MySQL

mybatis联合查询使用union,limit--order by不起作用

2017-11-09 10:50 591 查看
<select id="selectUnionData" resultMap="YszbResultMap" parameterType="java.util.List" >
select total.* from (
select '漂流浮标' as type,PLFBQZH as qzh,GCRQSJ as gcrqsj,QW as qw,QY as qy,HW as hw,HY as hy,FS as fs,FX as fx from nt_plfbyssjzb
<where>
<if test="dtBegin!=null and dtBegin !=''">
and GCRQSJ  >=  #{dtBegin}
</if>
<if test="dtEnd!=null and dtEnd !=''">
and GCRQSJ <=  #{dtEnd}
</if>
and PLFBQZH in
<foreach item="item" index="listPlfbqzh" collection="listPlfbqzh" open="(" separator="," close=")">
#{item}
</foreach>
</where>
union select '锚定浮标' as type,ZTH as qzh,YS01GCSJ as gcrqsj,YS03HBWD as qw,null as qy ,null as hw,YS08HSYD as hy,null as fs,null as fx from nt_hyqxyssjb
<where>
<if test="dtBegin!=null and dtBegin !=''">
and YS01GCSJ  >=  #{dtBegin}
</if>
<if test="dtEnd!=null and dtEnd !=''">
and YS01GCSJ <=  #{dtEnd}
</if>
and ZTH in
<foreach item="item" index="listMdfbqzh" collection="listMdfbqzh" open="(" separator="," close=")">
#{item}
</foreach>
</where>
) total
<if test="strCol!=null and strCol !=''">
ORDER BY ${strCol} ${strSort}
</if>
LIMIT #{nStart}, #{nLength}

</select>


一开始通过ORDER BY #{strCol} #{strSort} 来接收参数,但是查询的结果并没有排序,后来想到之前查询问题时有看到,#{strCol} #{strSort}接收的参数是带引号的,这边就换了${strCol}
${strSort}  试了一下,结果可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MySQL union limit