美文网首页RaspBerry PI
树莓派——mysql的学习(3)

树莓派——mysql的学习(3)

作者: 飞跑的蛤蟆 | 来源:发表于2016-11-21 23:01 被阅读17次
    mysql> -- 模式匹配
    mysql> -- 找出以‘b’开头的名字
    mysql> select * from pet where name like 'b%';
    +--------+--------+---------+------+------------+------------+
    | name   | owner  | species | sex  | birth      | death      |
    +--------+--------+---------+------+------------+------------+
    | Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
    | Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
    +--------+--------+---------+------+------------+------------+
    2 rows in set (0.01 sec)
    
    mysql> select * from pet where name like 'B%';
    +--------+--------+---------+------+------------+------------+
    | name   | owner  | species | sex  | birth      | death      |
    +--------+--------+---------+------+------------+------------+
    | Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
    | Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
    +--------+--------+---------+------+------------+------------+
    2 rows in set (0.00 sec)
    
    mysql> -- 不区分大小写
    mysql> -- 
    mysql> -- 
    mysql> -- 
    mysql> -- 找出以fy结尾的名字
    mysql> select * from pet where name like '%fy';
    +--------+--------+---------+------+------------+-------+
    | name   | owner  | species | sex  | birth      | death |
    +--------+--------+---------+------+------------+-------+
    | Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
    | Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
    +--------+--------+---------+------+------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> -- 找出包含'w'的名字
    mysql> select * from pet where name like '%w%';
    +----------+-------+---------+------+------------+------------+
    | name     | owner | species | sex  | birth      | death      |
    +----------+-------+---------+------+------------+------------+
    | Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
    | Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
    | Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
    +----------+-------+---------+------+------------+------------+
    3 rows in set (0.00 sec)
    
    mysql> -- 找出正好包含5个字符的名字,使用‘_’字符进行匹配
    mysql> select * from pet where name like '_____';
    +-------+--------+---------+------+------------+-------+
    | name  | owner  | species | sex  | birth      | death |
    +-------+--------+---------+------+------------+-------+
    | Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
    | Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
    +-------+--------+---------+------+------------+-------+
    2 rows in set (0.00 sec)
    
    

    由MySQL提供的模式匹配的其它类型是使用扩展正则表达式。当你对这类模式进行匹配测试时,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它们是同义词)。
    扩展正则表达式的一些字符是:
    · ‘.’匹配任何单个的字符。
    · 字符类“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的范围,使用一个“-”。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。
    · “ * ”匹配零个或多个在它前面的字符。例如,“x”匹配任何数量的“x”字符,“[0-9]”匹配任何数量的数字,而“.*”匹配任何数量的任何字符。
    如果REGEXP模式与被测试值的任何地方匹配,模式就匹配(这不同于LIKE模式匹配,只有与整个值匹配,模式才匹配)。
    为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用“^”或在模式的结尾用“$”。

    mysql> -- 找出以b开头的名字,使用‘^’匹配名字的开始
    mysql> select * from pet where name regexp '^b';
    +--------+--------+---------+------+------------+------------+
    | name   | owner  | species | sex  | birth      | death      |
    +--------+--------+---------+------+------------+------------+
    | Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
    | Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
    +--------+--------+---------+------+------------+------------+
    2 rows in set (0.00 sec)
    
    mysql> -- 强制让regexp比较区分大小写,使用binary关键字使其中的一个字符串变为二进制字符串mysql> -- 该查询只匹配名称^CCtrl-C -- exit!
    Aborted
    pi@raspberrypi:~ $ mysql -u zhang -p menagerie
    Enter password: 
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 38
    Server version: 5.5.52-0+deb8u1 (Raspbian)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> -- 该查询只匹配名称首字母的小写‘b’
    mysql> select * from pet where name recexp binary '^b';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'recexp binary '^b'' at line 1
    mysql> select * from pet where name regexp binary '^b';
    Empty set (0.01 sec)
    
    mysql> -- 查找以‘fy’结尾的名字,使用‘$’匹配名字的结尾
    mysql> select * from pet where name regexp 'fy$';
    +--------+--------+---------+------+------------+-------+
    | name   | owner  | species | sex  | birth      | death |
    +--------+--------+---------+------+------------+-------+
    | Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
    | Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
    +--------+--------+---------+------+------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> -- 找出包含一个‘w’的名字
    mysql> select * from pet where name regexp 'w';
    +----------+-------+---------+------+------------+------------+
    | name     | owner | species | sex  | birth      | death      |
    +----------+-------+---------+------+------------+------------+
    | Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
    | Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
    | Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
    +----------+-------+---------+------+------------+------------+
    3 rows in set (0.00 sec)
    
    mysql> -- 找出包含正好5个字符的名字,使用“^”和“$”匹配名字的开始和结尾,和5个“.”实例在两 者之间
    mysql> select * from pet where name regexp '^.....$';
    +-------+--------+---------+------+------------+-------+
    | name  | owner  | species | sex  | birth      | death |
    +-------+--------+---------+------+------------+-------+
    | Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
    | Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
    +-------+--------+---------+------+------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> -- 也可以使用“{n}”“重复n次”操作符重写前面的查询
    mysql> select * from pet where name regexp '^.{5}$';
    +-------+--------+---------+------+------------+-------+
    | name  | owner  | species | sex  | birth      | death |
    +-------+--------+---------+------+------------+-------+
    | Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
    | Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
    +-------+--------+---------+------+------------+-------+
    2 rows in set (0.00 sec)
    
    

    相关文章

      网友评论

        本文标题:树莓派——mysql的学习(3)

        本文链接:https://www.haomeiwen.com/subject/osqppttx.html