zoukankan      html  css  js  c++  java
  • 模糊、范围查询

    模糊查询

    一般模糊查询语句:

    select 字段 from 表 where 某字段 like 条件


    1.%专题

    sql语句做like模糊查询

    • like 'ab%'搜索以字符串ab开头的所有字符串

    • like '%ab%'搜索含有字符串ab的所有字符串

    • select * from user where username like '%单%' and username like '%身%'搜索含有"单"和"身"的所有字符串

    • select * from user where username like '%单%狗%'
      虽然能搜索出“单身狗”,但不能搜索出符合条件的“狗单身”。

    Mybatis中做like模糊查询

    • 参数中直接加入%%,比如
      user.setName("%三%")
        <!-- 查询名字含有三的用户 -->
    <select id="getuserByName" resultType="com.qf.pojo.User" parameterType="com.qf.pojo.User">
        select * from person where 1=1
    			<if test="username!=null and username ==''"> 
    			    and username LIKE #{username}
    			</if>
    			<if test="password!=null and password ==''">
    			    and password LIKE #{password}
    			</if>
    	
        </select>
    
    • 使用bind标签
    <select id="getUserByName" resultType="user" parameterType="com.qf.mybatisdemo.pojo.User">
            <!--username为实体类User对应的username属性 -->
            <bind name="pattern" value="'%' + username + '%'" />
            select username
            from user
            where username LIKE #{pattern}
    </select>
    

    使用bind标签的好处:使用 bind 拼接字符串不仅可以避免因更换数据库而修改 SQL,也能预防SQL注入。


    • CONCAT方法

    配置中sql可以这样写:where username LIKE concat(concat('%',#{username}),'%')


    • 严格大小写匹配

    方法:在查询字段前加binary就可以匹配大小写

      <!--当然数据库中的数据是不分大小写的-->
     select * from user where binary name like '%A%' <!--只匹配大写A-->  
     select * from user where binary name like '%a%' <!--只匹配小写a-->
    

    2._专题

    • select * from user where username like '_三_',找出中间为"三"的三个字的字符串

    3.[ ]专题

    [ ]说明:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。

    • select * from user where username like '[张李王]三'
      将找出“张三”、“李三”、“王三”(而不是“张李王三”)。
    • select * from user where username like '老[1-9]',将找出“老1”、“老2”、……、“老9”。

    如果[ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”


    4.[^]专题

    说明:表示不在括号所列之内的单个字符。其取值和[ ]相同,但它要求所匹配对象为指定字符以外的任一个字符。

    • select * from user where username like '[^张李王]三',将找出不姓“张三”、“李三”、“王三”等。
    • select * from user where username like '老[^1-4],将排除“老1”到“老4”,寻找“老5”、“老6”等。

    5.带符号模糊查询

    • like '5[%]'相当于查询含有5%的字符串
    • like '[_]n'相当于查询含有_n的字符串

    6.Escape 关键字

    说明:ESCAPE '/'的意思是"/"此时不是转义字符,而是普通字符。ESCAPE放在通配符前,表示将此通配符暂时定义为普通字符。

    • select * from user where username like '%6/%' ESCAPE '/' ,查询含有"6/"的字符串

    • select * from user where username like 'gs_' ESCAPE 's',查询含有"g_"的字符串,如果没有ESCAPE 's',那么查询的就是g再加上一个字符串。


    以上可以组合使用,如下:

    • like'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称。

    范围

    sql中范围查询

    1.数字

    • select * from user where age BETWEEN 20 and 24,查询20到24岁的用户

    • select * from user where age not BETWEEN 13 and 17,显示13到17岁之外的区间

    • select * from user where age in(13,17,23),显示age为13、17、23的用户

    • select * from age where (age BETWEEN 10 and 20) and not userid in (1,2,3),查询10到20岁但id不为1、2、3的用户

    2.时间

    • select * from user where birthday BETWEEN #07/04/1996# AND #07/09/1996#选取 user 介于 '04-July-1996' 和 '09-July-1996' 之间的出生日期的用户。

    Mybatis中范围查询

    1.数字

    1. 大于:&gt;
    2. 小于:&lt;
    3. 大于等于:&gt;=
    4. 小于等于:&lt;=

    还可以用标识比较大小

    2.时间

    • 时间类型为datetime或date

      • select * from user where <![CDATA[ and DATE_FORMAT(字段1, '%Y-%m-%d')>= DATE_FORMAT(#{birthday}, '%Y-%m-%d') ]]>,选取'字段1'日期大于'birthday'的用户
    • 时间类型为varchar

      • &lt;表示小于号
    • 还可以用order by 排序

    DATE_FORMAT(NOW(),'%m-%d-%Y'),now()获取当前时间

  • 相关阅读:
    【转】测试人员职业规划
    phantomjs处理alert、confirm弹窗
    linux搭建phantomjs+webdriver+testng+ant自动化工程
    linux搭建apache服务并修改默认路径
    linux环境vnc部署过程详解
    mongodb集群+分片部署(二)
    mongodb部署单节点(一)
    java javaScript实现遮罩层 动态加载
    感受
    JavaScript之中Array用法的一些技巧总结
  • 原文地址:https://www.cnblogs.com/qiyiguoandqijiguo/p/10841453.html
Copyright © 2011-2022 走看看