机智的爆爆哥 2020-03-30 13:27 采纳率: 33.3%
浏览 222
已采纳

想要多个条件来查询对应的记录 起始时间 结束时间 用户名 状态

 SELECT
            u.id,u.username,u.password,u.phone,u.state,u.regdate,
            r.id rid,r.rolename,r.code
        FROM
            USER u,role r,user_role ur
        WHERE
            ur.uid=u.id
        AND
            ur.rid=r.id
        AND
         1=1
        and(username like #{info} or phone like #{info})
        or
            state=#{state}
        or
            regdate
        BETWEEN
            #{startdate}
        AND
            #{enddate}

发现出了问题 用的是mybatis 主要是连接时or和and该怎么用不是很清楚
且当某项为空时 就忽略

SELECT
        u.id,u.username,u.password,u.phone,u.state,u.regdate,
        r.id rid,r.rolename,r.code
        FROM
        USER u,role r,user_role ur
        <where>
            ur.uid=u.id
            AND
            ur.rid=r.id
            AND
            1=1
            <if test="info!=null and info !=&quot;&quot;">
                and (
                username like #{info}
                or phone like #{info}
                )
            </if>
            <if test="state!=null and state !=&quot;&quot;">
                and state=#{state}
            </if>
            <if test="startdate!=null and startdate !=&quot;&quot;">
                and regdate>=#{startdate}
            </if>
            <if test="enddate!=null and enddate !=&quot;&quot;">
                and #{enddate}>regdate
            </if>

        </where>

补充了 看下

已经解决啦 现把没问题的代码放上 有需要可以参考下

<select id="getPageInfo" resultMap="userRoleMap">
        SELECT
        u.id,u.username,u.password,u.phone,u.state,u.regdate,
        r.id,r.rolename,r.code
        FROM
        USER u,role r,user_role ur
        <where>
            ur.uid=u.id
            AND
            ur.rid=r.id
            and
            1=1
            <if test="info!='%%'">
                and (
                u.username like #{info}
                or u.phone like #{info}
                )
            </if>

            <if test="state!=null and state!=&quot;&quot;">
                and u.state=#{state}
            </if>
            <if test="startdate!=null and startdate !=&quot;&quot;">
                and u.regdate &gt;=#{startdate}
            </if>
            <if test="enddate!=null and enddate !=&quot;&quot;">
                and #{enddate} &gt;u.regdate
            </if>

        </where>
    </select>
    <resultMap id="userRoleMap" type="cn.bb.bbtravel.model.User">
        <id column="id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="phone" property="phone"></result>
        <result column="state" property="state"></result>
        <result column="regdate" property="regdate"></result>
        <collection property="roles" ofType="cn.bb.bbtravel.model.Role">
            <id column="rid" property="id"></id>
            <result column="rolename" property="rolename"></result>
            <result column="code" property="code"></result>
        </collection>
    </resultMap>
  • 写回答

2条回答 默认 最新

  • 小鑫lay 2020-03-30 13:35
    关注

    and是与运算,or是或运算,or的运算级最低,判空的话可以用

    <if test="state != null and state != ''" >
                        and state =#{state}
    </if>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!