机智的爆爆哥 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条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)