mybatis mysql union all用法

XML配置代码如下:

union  all查询的时候注意的有两点

1:union  al前后两个子部分的查询的字段要一致

2:注意给子部分取别名


<select id="findFarmByUserAndAuthor" resultMap="FarmListMap"

        parameterType="java.lang.String">
            select t3.farmid farmid,t3.strname strname,t3.state state,t3.type type,t3.strPhoneId strPhoneId from (
                select t1.farmid,t1.strname,t1.state,t1.type,t1.strPhoneId from (
                     select f.id farmid,f.strName strname,f.state state,f.type type,
                    (select
                    t.strPhoneId from termuser t where
                    t.id=f.iUserId)strphoneid
                    from farm f where f.iUserId =#{userId}) as t1
            UNION ALL
            select t2.farmid,t2.strname,t2.state,t2.type,t2.strPhoneId from (
                    select f.id farmid,f.strName strname,f.state state,f.type type,(select
                            t.strPhoneId from termuser t where
                            t.id=f.iUserId)strphoneid from author a
            LEFT JOIN farm f ON f.ID = a.FARM_ID
            where a.AUTHORED_ID = #{userId}) as t2)as t3 where
            <if test="strName != null and '' != strName">
              t3.strname like CONCAT(CONCAT('%', #{strName}), '%')  
            </if>
            limit #{curSize},#{pageSize}
    </select>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值