zoukankan      html  css  js  c++  java
  • Mysql必知必会学习笔记(六)

    LIKE操作符

    通配符(wildcard) 用来匹配值的一部分的特殊字符。
    搜索模式(search pattern)由字面值、通配符或两者组合构成的搜索条件。

    通配符本身实际是SQL的WHERE子句中有特殊含义的字符,SQL支持几种通配符。
     (1)百分号(%)通配符
    为在搜索子句中使用通配符,必须使用LIKE操作符。LIKE指示MySQL,后跟的搜索模式利用通配符匹配而不是直接相等匹配进行比较。
    区分大小写 根据MySQL的配置方式,搜索可以是区分大小写的。如果区分大小写,'jet%'与JetPack 1000将不匹配。
    在搜索中,%表示任何字符出现任意次数。例如,为了找出所有以词jet起头的产品,可使用以下select语句

    %是接受jet之后的任意字符,不管它有多少字符
    mysql> select vend_id,prod_name from products where prod_name like 'Jet%' ;
    +---------+--------------+
    | vend_id | prod_name    |
    +---------+--------------+
    |    1005 | JetPack 1000 |
    |    1005 | JetPack 2000 |
    +---------+--------------+
    2 rows in set (0.00 sec)
    
    mysql> select vend_id,prod_name from products where prod_name like 'jet%' ;
    +---------+--------------+
    | vend_id | prod_name    |
    +---------+--------------+
    |    1005 | JetPack 1000 |
    |    1005 | JetPack 2000 |
    +---------+--------------+
    2 rows in set (0.00 sec)

     通配符可以在搜索模式中任意位置使用,可以使用多个通配符


    select prod_id, prod_name from products where prod_name like '%anvil%';
    mysql> select prod_id, prod_name from products where prod_name like '%anvil%';
    +---------+--------------+
    | prod_id | prod_name    |
    +---------+--------------+
    | ANV01   | .5 ton anvil |
    | ANV02   | 1 ton anvil  |
    | ANV03   | 2 ton anvil  |
    +---------+--------------+
    3 rows in set (0.00 sec)

    找出所有以s起头以e结尾的所有产品

    select prod_id, prod_name from products where prod_name like 's%e';
    mysql> select prod_id, prod_name from products where prod_name like 's%e';
    +---------+-----------+
    | prod_id | prod_name |
    +---------+-----------+
    | SAFE    | Safe      |
    +---------+-----------+
    1 row in set (0.00 sec)
    如果要搜索的数据,最后有一个尾空格,会影响通配符的使用,解决办法就是在末尾加一个%

    %通配符可以匹配任何东西,但是不匹配NULL
     要的是要注意到,除了一个或多个字符外,%还能匹配0个字符。%代表搜索模式中给定位置的0个、1个或多个字符。

    (2)下划线(_)通配符

    与%用途一样,但是下划线匹配单个字符而不是多个字符

    显示“ ton anvil”中只存在一个字符的字段

    select prod_id, prod_name from products where prod_name like '_ ton anvil';
    +---------+-------------+
    | prod_id | prod_name   |
    +---------+-------------+
    | ANV02   | 1 ton anvil |
    | ANV03   | 2 ton anvil |
    +---------+-------------+
    2 rows in set (0.00 sec)
    
    mysql> select prod_id, prod_name from products where prod_name like '% ton anvil';
    +---------+--------------+
    | prod_id | prod_name    |
    +---------+--------------+
    | ANV01   | .5 ton anvil |
    | ANV02   | 1 ton anvil  |
    | ANV03   | 2 ton anvil  |
    +---------+--------------+
    3 rows in set (0.00 sec)
    2、使用通配符的技巧

    (1)不要过度使用通配符,能不用就不用

    (2)把通配符置于搜索模式开始处,搜索起来最慢

    (3)要注意通配符位置,放错地方,可能不会返回预期效果

     第9章 用正则表达式进行搜索

    1、使用MySQL正则表达式

    (1)基本字符匹配

    检索列prod_name包含文本1000的所有行

    select prod_name from products where prod_name regexp '1000' order by prod_name;
    mysql> select prod_name from products where prod_name regexp '1000' order by prod_name;
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    +--------------+
    1 row in set (0.00 sec)

    关键字regexp后所跟的东西作为正则表达式,与文字正文1000匹配的一个正则表达式处理

    使用正则表达式.000  . 是正则表达式语言中搞一个特殊的字符。表示匹配任意一个字符,因此1000和2000都返回

    select prod_name from products where prod_name regexp '.000' order by prod_name;
    mysql> select prod_name from products where prod_name regexp '.000' order by prod_name;
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    | JetPack 2000 |
    +--------------+
    2 rows in set (0.00 sec)
    LIKE和REGEXP之间有一个重要差别

    select prod_name from products where prod_name like '1000' order by prod_name;
    mysql> select prod_name from products where prod_name like '1000' order by prod_name;
    Empty set (0.00 sec)
    此处不返回数据,like匹配整个列。如果匹配的文本仅是在列中出现(就是不完全相同),like不会找到它

    select prod_name from products where prod_name regexp '1000' order by prod_name;
    mysql> select prod_name from products where prod_name regexp '1000' order by prod_name;
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    +--------------+
    1 row in set (0.00 sec)
    而regexp实在列值内进行匹配,如果被匹配的文本在列值中出现,regexp会找到它,相应行会被返回。

    用regexp也可以用来匹配整个列(从而和like相同),使用^和$定位符即可

    在MySQL中的正则表达式匹配不区分大小写,为区分大小写,可使用binary关键字,如下

    select prod_name from products where prod_name regexp binary 'JetPack .000';
    mysql> select prod_name from products where prod_name regexp 'JetPack .000';
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    | JetPack 2000 |
    +--------------+
    2 rows in set (0.00 sec)
    
    mysql> select prod_name from products where prod_name regexp binary 'jetPack .000';
    Empty set (0.00 sec)
    (2)进行OR匹配

    为搜索两个串之一,使用|,如下所示

    select prod_name from products where prod_name regexp '1000|2000' order by prod_name;
    mysql> select prod_name from products where prod_name regexp '1000|2000' order by prod_name;
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    | JetPack 2000 |
    +--------------+
    2 rows in set (0.00 sec)
    两个以上or条件,例如‘1000|2000|3000’,将匹配1000或2000或3000

    (3)匹配几个字符之一

    如何匹配特定字符,用 [ 和 ] 括起来的字符完成,如下所示

    select prod_name from products where prod_name regexp ‘[123] Ton’ order by prod_name;

    使用正则表达式[123],定义了一组字符,意思是匹配1或2或3

    (4)匹配范围

    集合可以用定义要匹配的一个或多个字符,如[0123456789]会匹配数字0到9,为了简化,可以写成[0-9]

    select prod_name from products where prod_name regexp ‘[1-5] Ton’ order by prod_name;

    (5)匹配特殊字符

    为了匹配特殊字符,必须用 \ 为前导,\- 表示 -,\. 表示.

    select prod_name from vendors where vend_name regexp ‘\.’ order by vend_name;

    (6)匹配字符类

    (7)匹配多个实例

    select prod_name from products where prod_name regexp ‘\([0-9) sticks?\)’ order by prod_name;

    正则表达式\([0-9) sticks?\),[0-9]匹配任意字符,sticks?匹配stick和sticks(s后的?使s可选)

    select prod_name from products where prod_name regexp ‘[[:digit:]]{4}’ order by prod_name;

    如前所述, [:digit:]匹配任意数字, 因而它为数字的 个集合。 {4}确切地要求它前面的字符(任意数字) 出现4次, 所以[[:digit:]]

    {4}匹配连在 起的任意4位数字。

    (8)定位符

    select prod_name from products where prod_name regexp ‘A[0-9\.]’ order by prod_name;

    ^匹配串的开始。因此,"[0-9\.]只在.或任意数字为串中第一个字符时才匹配它们。没有^,则还要多检索出4个别的行(那些中间有数字的行)。
     
    ————————————————
    版权声明:本文为CSDN博主「搞IT的王蜀黍」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/yilouwen7522/article/details/81091139
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    php 匿名函数和闭包
    项目在线压缩js
    USACOTrainning.The Clocks
    USACOTrainning.Mother's Milk
    c# TXT文件读写
    Access以及少量Regex
    USACOTraining.Packing Rectangles
    First
    CUGBLinker and EXE
    异常处理总结
  • 原文地址:https://www.cnblogs.com/laonicc/p/12509506.html
Copyright © 2011-2022 走看看