《MySQL必知必会》3-9章笔记

其实就是把书上的例子给敲了一遍

1.连接、选择与显示

①连接MySQL

方式一:在命令提示符中输入


mysql -u root -p

登录root用户并输入密码

方式二:在MySQL文件夹内打开MySQL命令客户端

方式三:用navicat或sqlyog之类的图形化工具连接数据库,连接时输入用户名和密码

②选择数据库

查看一个数据库中的数据,或是对这个数据库进行操作,必须要先对这个数据库进行选择操作


USE DB1;

以上语句选择了名为DB1的数据库

③显示数据库和表的详细信息

  • 使用show命令显示所有可用的数据库

show databases;#用这句来显示所有可用数据库的列表

+--------------------+

| Database          |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

  • 使用show显示指定数据库中所有的表

mysql> use mysql;

Database changed

mysql> show tables;#显示一个数据库中表的列表

+---------------------------+

| Tables_in_mysql          |

+---------------------------+

| columns_priv              |

| component                |

| db                        |

| default_roles            |

| engine_cost              |

| func                      |

| general_log              |

| global_grants            |

| gtid_executed            |

| help_category            |

| help_keyword              |

| help_relation            |

| help_topic                |

| innodb_index_stats        |

| innodb_table_stats        |

| password_history          |

| plugin                    |

| procs_priv                |

| proxies_priv              |

| role_edges                |

| server_cost              |

| servers                  |

| slave_master_info        |

| slave_relay_log_info      |

| slave_worker_info        |

| slow_log                  |

| tables_priv              |

| time_zone                |

| time_zone_leap_second    |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

  • 使用show columns显示出一个表的所有字段的详细信息

mysql> show columns from db;

+-----------------------+---------------+------+-----+---------+-------+

| Field                | Type          | Null | Key | Default | Extra |

+-----------------------+---------------+------+-----+---------+-------+

| Host                  | char(255)    | NO  | PRI |        |      |

| Db                    | char(64)      | NO  | PRI |        |      |

| User                  | char(32)      | NO  | PRI |        |      |

| Select_priv          | enum('N','Y') | NO  |    | N      |      |

| Insert_priv          | enum('N','Y') | NO  |    | N      |      |

| Update_priv          | enum('N','Y') | NO  |    | N      |      |

| Delete_priv          | enum('N','Y') | NO  |    | N      |      |

| Create_priv          | enum('N','Y') | NO  |    | N      |      |

| Drop_priv            | enum('N','Y') | NO  |    | N      |      |

| Grant_priv            | enum('N','Y') | NO  |    | N      |      |

| References_priv      | enum('N','Y') | NO  |    | N      |      |

| Index_priv            | enum('N','Y') | NO  |    | N      |      |

| Alter_priv            | enum('N','Y') | NO  |    | N      |      |

| Create_tmp_table_priv | enum('N','Y') | NO  |    | N      |      |

| Lock_tables_priv      | enum('N','Y') | NO  |    | N      |      |

| Create_view_priv      | enum('N','Y') | NO  |    | N      |      |

| Show_view_priv        | enum('N','Y') | NO  |    | N      |      |

| Create_routine_priv  | enum('N','Y') | NO  |    | N      |      |

| Alter_routine_priv    | enum('N','Y') | NO  |    | N      |      |

| Execute_priv          | enum('N','Y') | NO  |    | N      |      |

| Event_priv            | enum('N','Y') | NO  |    | N      |      |

| Trigger_priv          | enum('N','Y') | NO  |    | N      |      |

+-----------------------+---------------+------+-----+---------+-------+

也可用describe达到同样效果


mysql> describe db;

+-----------------------+---------------+------+-----+---------+-------+

| Field                | Type          | Null | Key | Default | Extra |

+-----------------------+---------------+------+-----+---------+-------+

| Host                  | char(255)    | NO  | PRI |        |      |

| Db                    | char(64)      | NO  | PRI |        |      |

| User                  | char(32)      | NO  | PRI |        |      |

| Select_priv          | enum('N','Y') | NO  |    | N      |      |

| Insert_priv          | enum('N','Y') | NO  |    | N      |      |

| Update_priv          | enum('N','Y') | NO  |    | N      |      |

| Delete_priv          | enum('N','Y') | NO  |    | N      |      |

| Create_priv          | enum('N','Y') | NO  |    | N      |      |

| Drop_priv            | enum('N','Y') | NO  |    | N      |      |

| Grant_priv            | enum('N','Y') | NO  |    | N      |      |

| References_priv      | enum('N','Y') | NO  |    | N      |      |

| Index_priv            | enum('N','Y') | NO  |    | N      |      |

| Alter_priv            | enum('N','Y') | NO  |    | N      |      |

| Create_tmp_table_priv | enum('N','Y') | NO  |    | N      |      |

| Lock_tables_priv      | enum('N','Y') | NO  |    | N      |      |

| Create_view_priv      | enum('N','Y') | NO  |    | N      |      |

| Show_view_priv        | enum('N','Y') | NO  |    | N      |      |

| Create_routine_priv  | enum('N','Y') | NO  |    | N      |      |

| Alter_routine_priv    | enum('N','Y') | NO  |    | N      |      |

| Execute_priv          | enum('N','Y') | NO  |    | N      |      |

| Event_priv            | enum('N','Y') | NO  |    | N      |      |

| Trigger_priv          | enum('N','Y') | NO  |    | N      |      |

+-----------------------+---------------+------+-----+---------+-------+

  • 其它常用show语句

show status;#用于显示广泛的服务起状态信息

show create database;#显示创建特定数据库的SQL语句

show create table;#显示创建特定表的SQL语句

show grants;#显示授予用户的安装权限

show errors;

show warnings;#显示服务器错误或警告信息

2.检索数据

使用select语句检索数据

①检索单个列


mysql> select prod_name from products;#从product表中检索一个名为prod_name的列

+----------------+

| prod_name      |

+----------------+

| .5 ton anvil  |

| 1 ton anvil    |

| 2 ton anvil    |

| Detonator      |

| Bird seed      |

| Carrots        |

| Fuses          |

| JetPack 1000  |

| JetPack 2000  |

| Oil can        |

| Safe          |

| Sling          |

| TNT (1 stick)  |

| TNT (5 sticks) |

+----------------+

这将返回表中所有行

②检索多个列

在select之后给出多个列名


mysql> select prod_id,prod_name,prod_price from products;

+---------+----------------+------------+

| prod_id | prod_name      | prod_price |

+---------+----------------+------------+

| ANV01  | .5 ton anvil  |      5.99 |

| ANV02  | 1 ton anvil    |      9.99 |

| ANV03  | 2 ton anvil    |      14.99 |

| DTNTR  | Detonator      |      13.00 |

| FB      | Bird seed      |      10.00 |

| FC      | Carrots        |      2.50 |

| FU1    | Fuses          |      3.42 |

| JP1000  | JetPack 1000  |      35.00 |

| JP2000  | JetPack 2000  |      55.00 |

| OL1    | Oil can        |      8.99 |

| SAFE    | Safe          |      50.00 |

| SLING  | Sling          |      4.49 |

| TNT1    | TNT (1 stick)  |      2.50 |

| TNT2    | TNT (5 sticks) |      10.00 |

+---------+----------------+------------+

③检索所有列

用*通配符来检索所有列


mysql> select * from products;

+---------+---------+----------------+------------+----------------------------------------------------------------+

| prod_id | vend_id | prod_name      | prod_price | prod_desc                                                      |

+---------+---------+----------------+------------+----------------------------------------------------------------+

| ANV01  |    1001 | .5 ton anvil  |      5.99 | .5 ton anvil, black, complete with handy hook                  |

| ANV02  |    1001 | 1 ton anvil    |      9.99 | 1 ton anvil, black, complete with handy hook and carrying case |

| ANV03  |    1001 | 2 ton anvil    |      14.99 | 2 ton anvil, black, complete with handy hook and carrying case |

| DTNTR  |    1003 | Detonator      |      13.00 | Detonator (plunger powered), fuses not included                |

| FB      |    1003 | Bird seed      |      10.00 | Large bag (suitable for road runners)                          |

| FC      |    1003 | Carrots        |      2.50 | Carrots (rabbit hunting season only)                          |

| FU1    |    1002 | Fuses          |      3.42 | 1 dozen, extra long                                            |

| JP1000  |    1005 | JetPack 1000  |      35.00 | JetPack 1000, intended for single use                          |

| JP2000  |    1005 | JetPack 2000  |      55.00 | JetPack 2000, multi-use                                        |

| OL1    |    1002 | Oil can        |      8.99 | Oil can, red                                                  |

| SAFE    |    1003 | Safe          |      50.00 | Safe with combination lock                                    |

| SLING  |    1003 | Sling          |      4.49 | Sling, one size fits all                                      |

| TNT1    |    1003 | TNT (1 stick)  |      2.50 | TNT, red, single stick                                        |

| TNT2    |    1003 | TNT (5 sticks) |      10.00 | TNT, red, pack of 10 sticks                                    |

+---------+---------+----------------+------------+----------------------------------------------------------------+

④检索不同的行

使用distinct关键字使得查询结果只能是不同的值

不使用distinct查询的结果


mysql> select vend_id from products;

+---------+

| vend_id |

+---------+

|    1001 |

|    1001 |

|    1001 |

|    1002 |

|    1002 |

|    1003 |

|    1003 |

|    1003 |

|    1003 |

|    1003 |

|    1003 |

|    1003 |

|    1005 |

|    1005 |

+---------+

用了distinct之后的查询结果


mysql> select distinct vend_id from products;

+---------+

| vend_id |

+---------+

|    1001 |

|    1002 |

|    1003 |

|    1005 |

+---------+

⑤限制结果

使用limit关键字限制查询结果的行数

  • 显示前五行

mysql> select prod_name from products limit 5;

+--------------+

| prod_name    |

+--------------+

| .5 ton anvil |

| 1 ton anvil  |

| 2 ton anvil  |

| Detonator    |

| Bird seed    |

+--------------+

  • 显示从第五行开始的往后五行(六到十行)

mysql> select prod_name from products limit 5,5;

+--------------+

| prod_name    |

+--------------+

| Carrots      |

| Fuses        |

| JetPack 1000 |

| JetPack 2000 |

| Oil can      |

+--------------+

3.排序检索数据

①排序数据

使用order by子句进行排序


mysql> select prod_name from products order by prod_name;#对结果按prod_name列按字母顺序排序

+----------------+

| prod_name      |

+----------------+

| .5 ton anvil  |

| 1 ton anvil    |

| 2 ton anvil    |

| Bird seed      |

| Carrots        |

| Detonator      |

| Fuses          |

| JetPack 1000  |

| JetPack 2000  |

| Oil can        |

| Safe          |

| Sling          |

| TNT (1 stick)  |

| TNT (5 sticks) |

+----------------+

②按多个列排序

order by子句中多个列名之间用逗号隔开,按照子句中指定的顺序进行排序


#先按照prod_price从小到大进行排序

#在prod_price相同的情况下再按照prod_name的字母顺序进行排序

mysql> select prod_id,prod_price,prod_name from products order by prod_price,prod_name;

+---------+------------+----------------+

| prod_id | prod_price | prod_name      |

+---------+------------+----------------+

| FC      |      2.50 | Carrots        |

| TNT1    |      2.50 | TNT (1 stick)  |

| FU1    |      3.42 | Fuses          |

| SLING  |      4.49 | Sling          |

| ANV01  |      5.99 | .5 ton anvil  |

| OL1    |      8.99 | Oil can        |

| ANV02  |      9.99 | 1 ton anvil    |

| FB      |      10.00 | Bird seed      |

| TNT2    |      10.00 | TNT (5 sticks) |

| DTNTR  |      13.00 | Detonator      |

| ANV03  |      14.99 | 2 ton anvil    |

| JP1000  |      35.00 | JetPack 1000  |

| SAFE    |      50.00 | Safe          |

| JP2000  |      55.00 | JetPack 2000  |

+---------+------------+----------------+

③指定排序方向

DESC:降序 ASC:升序


#对查询结果按照价格从高到底进行排序

mysql> select prod_id,prod_price,prod_name

    -> from products

    -> order by prod_price desc;

+---------+------------+----------------+

| prod_id | prod_price | prod_name      |

+---------+------------+----------------+

| JP2000  |      55.00 | JetPack 2000  |

| SAFE    |      50.00 | Safe          |

| JP1000  |      35.00 | JetPack 1000  |

| ANV03  |      14.99 | 2 ton anvil    |

| DTNTR  |      13.00 | Detonator      |

| FB      |      10.00 | Bird seed      |

| TNT2    |      10.00 | TNT (5 sticks) |

| ANV02  |      9.99 | 1 ton anvil    |

| OL1    |      8.99 | Oil can        |

| ANV01  |      5.99 | .5 ton anvil  |

| SLING  |      4.49 | Sling          |

| FU1    |      3.42 | Fuses          |

| FC      |      2.50 | Carrots        |

| TNT1    |      2.50 | TNT (1 stick)  |

+---------+------------+----------------+

先队产品价格进行降序排序,再对产品名排序:


mysql> select prod_id,prod_price,prod_name

    -> from products

    -> order by prod_price desc,prod_name;

+---------+------------+----------------+

| prod_id | prod_price | prod_name      |

+---------+------------+----------------+

| JP2000  |      55.00 | JetPack 2000  |

| SAFE    |      50.00 | Safe          |

| JP1000  |      35.00 | JetPack 1000  |

| ANV03  |      14.99 | 2 ton anvil    |

| DTNTR  |      13.00 | Detonator      |

| FB      |      10.00 | Bird seed      |

| TNT2    |      10.00 | TNT (5 sticks) |

| ANV02  |      9.99 | 1 ton anvil    |

| OL1    |      8.99 | Oil can        |

| ANV01  |      5.99 | .5 ton anvil  |

| SLING  |      4.49 | Sling          |

| FU1    |      3.42 | Fuses          |

| FC      |      2.50 | Carrots        |

| TNT1    |      2.50 | TNT (1 stick)  |

+---------+------------+----------------+

与limit配合查出最昂贵物品的价格


mysql> select prod_price from products order by prod_price desc limit 1;

+------------+

| prod_price |

+------------+

|      55.00 |

+------------+

4.过滤数据

where子句在order by子句之前

①where子句


#查询价格为2.5的商品

mysql> select prod_name,prod_price from products where prod_price = 2.50;

+---------------+------------+

| prod_name    | prod_price |

+---------------+------------+

| Carrots      |      2.50 |

| TNT (1 stick) |      2.50 |

+---------------+------------+

②条件操作符

操作符说明

=:等于

<>:不等于

!=:不等于

<:小于

<=:小于等于

>:大于

>=:大于等于

BETWEEN...AND...:在两个值之间

③检查单个值

  • MySQL执行匹配时默认不区分大小写

mysql> select prod_name,prod_price from products where prod_name = 'fuses';

+-----------+------------+

| prod_name | prod_price |

+-----------+------------+

| Fuses    |      3.42 |

+-----------+------------+

④不匹配检查


#查询不是由1003供应商供应的商品,也可以用<>符号

mysql> select vend_id,prod_name from products where vend_id != 1003;

+---------+--------------+

| vend_id | prod_name    |

+---------+--------------+

|    1001 | .5 ton anvil |

|    1001 | 1 ton anvil  |

|    1001 | 2 ton anvil  |

|    1002 | Fuses        |

|    1002 | Oil can      |

|    1005 | JetPack 1000 |

|    1005 | JetPack 2000 |

+---------+--------------+

  • 字符串要用单引号' '括起来

⑤范围值检查


#查询价格5到10之间的商品

mysql> select prod_name,prod_price from products where prod_price between 5 and 10;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| .5 ton anvil  |      5.99 |

| 1 ton anvil    |      9.99 |

| Bird seed      |      10.00 |

| Oil can        |      8.99 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+

⑥空值检查

使用is null来检查空值


#查询没有邮箱的顾客id

mysql> select cust_id from customers where cust_email is null;

+---------+

| cust_id |

+---------+

|  10002 |

|  10005 |

+---------+

5.where子句的进阶用法

①and操作符

用and对多个列进行过滤


mysql> select prod_id,prod_name,prod_price from products where vend_id = 1003 and prod_price <= 10;

+---------+----------------+------------+

| prod_id | prod_name      | prod_price |

+---------+----------------+------------+

| FB      | Bird seed      |      10.00 |

| FC      | Carrots        |      2.50 |

| SLING  | Sling          |      4.49 |

| TNT1    | TNT (1 stick)  |      2.50 |

| TNT2    | TNT (5 sticks) |      10.00 |

+---------+----------------+------------+

②or操作符


mysql> select prod_name,prod_price from products where vend_id = 1002 or vend_id = 1003;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| Fuses          |      3.42 |

| Oil can        |      8.99 |

| Detonator      |      13.00 |

| Bird seed      |      10.00 |

| Carrots        |      2.50 |

| Safe          |      50.00 |

| Sling          |      4.49 |

| TNT (1 stick)  |      2.50 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+

③计算次序

  • 列出价格大于等于10美元且由1002或1003供应商制造的所有商品

mysql> select prod_name,prod_price from products where vend_id=1002 or vend_id=1003 and prod_price >= 10;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| Fuses          |      3.42 |

| Oil can        |      8.99 |

| Detonator      |      13.00 |

| Bird seed      |      10.00 |

| Safe          |      50.00 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+

这样的查询结果显然不符合要求(结果中由价格小于10的商品),原因是and的运算优先度比or高,MySQL把这段条件解释为“供应商1003制造的价格大于等于10的商品或1002制造的所有商品”了

为了避免这种情况应该使用括号


mysql> select prod_name,prod_price from products where (vend_id=1002 or vend_id=1003) and prod_price >= 10;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| Detonator      |      13.00 |

| Bird seed      |      10.00 |

| Safe          |      50.00 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+

④in操作符

用in操作符和括号配合指定条件范围,范围中的每个条件都可以进行匹配,条件之间用逗号分隔


#和下面的or达到了一样的效果

mysql> select prod_name,prod_price from products where vend_id in (1002,1003) order by prod_name;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| Bird seed      |      10.00 |

| Carrots        |      2.50 |

| Detonator      |      13.00 |

| Fuses          |      3.42 |

| Oil can        |      8.99 |

| Safe          |      50.00 |

| Sling          |      4.49 |

| TNT (1 stick)  |      2.50 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+


mysql> select prod_name,prod_price from products where vend_id = 1002 or vend_id = 1003 order by prod_name;

+----------------+------------+

| prod_name      | prod_price |

+----------------+------------+

| Bird seed      |      10.00 |

| Carrots        |      2.50 |

| Detonator      |      13.00 |

| Fuses          |      3.42 |

| Oil can        |      8.99 |

| Safe          |      50.00 |

| Sling          |      4.49 |

| TNT (1 stick)  |      2.50 |

| TNT (5 sticks) |      10.00 |

+----------------+------------+

推荐使用in操作符来代替or操作符,原因如下:

  • 清楚直观

  • 计算次序容易管理

  • 比or执行速度更快

  • in的最大优点是可以包含其它where子句

⑤not操作符

用not否定后面跟的条件


#列出1002,1003以外的供应商制造的所有商品

mysql> select prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;

+--------------+------------+

| prod_name    | prod_price |

+--------------+------------+

| .5 ton anvil |      5.99 |

| 1 ton anvil  |      9.99 |

| 2 ton anvil  |      14.99 |

| JetPack 1000 |      35.00 |

| JetPack 2000 |      55.00 |

+--------------+------------+

6.用通配符进行过滤

①like操作符

  • 通配符:用来匹配值的一部分的特殊字符

  • 搜索模式:由字面值、通配符或两者组合构成的搜索条件

为了使用通配符,必须使用like操作符

②百分号通配符

%表示任何字符出现任意次数


#列出以jet开头的所有商品

mysql> select prod_id,prod_name from products where prod_name like 'jet%';

+---------+--------------+

| prod_id | prod_name    |

+---------+--------------+

| JP1000  | JetPack 1000 |

| JP2000  | JetPack 2000 |

+---------+--------------+

可以使用多个通配符


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  |

+---------+--------------+

③下划线通配符

用于匹配单个字符


mysql> 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 |

+---------+-------------+

④使用通配符的技巧

  • 如果其它操作符能达到相同目的就不要用通配符

  • 尽量不要把通配符放在搜索模式的开始处,这样搜索速度最慢

  • 注意通配符的位置

7.用正则表达式进行搜索

正则表达式是用来匹配文本的特殊的串

①基本字符匹配


mysql> select prod_name from products where prod_name regexp '1000' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| JetPack 1000 |

+--------------+

regexp后所跟的是正则表达式

用.去匹配任意一个字符


mysql> select prod_name from products where prod_name regexp '.000' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| JetPack 1000 |

| JetPack 2000 |

+--------------+

②进行or匹配


mysql> select prod_name from products where prod_name regexp '1000|2000' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| JetPack 1000 |

| JetPack 2000 |

+--------------+

③匹配几个字符之一


#[123]匹配1or2or3

mysql> select prod_name from products where prod_name regexp '[123] Ton' order by prod_name;

+-------------+

| prod_name  |

+-------------+

| 1 ton anvil |

| 2 ton anvil |

+-------------+

④匹配范围

用[0-9]表示[0123456789]


mysql> select prod_name from products where prod_name regexp '[1-5] Ton' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| .5 ton anvil |

| 1 ton anvil  |

| 2 ton anvil  |

+--------------+

⑤匹配特殊字符

用转移符号\匹配特殊字符


#匹配.

mysql> select vend_name from vendors where vend_name regexp '\\.' order by vend_name;

+--------------+

| vend_name    |

+--------------+

| Furball Inc. |

+--------------+


\\-匹配-,\\.匹配.,\\\匹配\

⑥匹配字符类

预定义的字符集称为字符类

类的说明

[:alnum:]            任意字母和数字          

 [:alpha:]              任意字符            

 [:blank:]              空格和制表            

 [:cntrl:]            ascⅡ控制字符          

 [:digit:]              任意数字            

 [:graph:]    与[:print:]相同但不包含空格    

 [:lower:]            任意小写字符          

 [:print:]            任意可打印字符          

 [:punct:]   不在[:alnum:]和[:cntrl:]中的字符 

| [:space:]      包括空格在内的任意空白字符    

| [:upper:]            任意大写字母          

| [:xdigit:]         任意十六进制数字        

⑦匹配多个实例

重复元字符

元字符的说明

  *          0个或多个匹配        

  +      一个或多个匹配,等于{1,}  

  ?            0个或1个匹配        

  {n}        指定数目的匹配      

  {n,}      不少于指定数目的匹配    

 {n,m}   匹配数目的范围(m不超过255) 

例子一:


#\\(和\\)分别匹配了括号的两边,s后的?使得s可有可无

mysql> select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name;

+----------------+

| prod_name      |

+----------------+

| TNT (1 stick)  |

| TNT (5 sticks) |

+----------------+

例子二:


#[:digit:]表示任意数字,{4}表示任意数字连续出现四次

mysql> select prod_name from products where prod_name regexp '[[:digit:]]{4}' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| JetPack 1000 |

| JetPack 2000 |

+--------------+

#也可以这样写

mysql> select prod_name from products where prod_name regexp '[0-9][0-9][0-9][0-9]' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| JetPack 1000 |

| JetPack 2000 |

+--------------+

⑧定位符

定位元字符

 元字符      说明    

    ^     文本的开始 

    $     文本的结尾 

 [[:<:]]   词的开始  

 [[:>:]]   词的结尾  

#搜索一个数(包括以小数点开始的数)开始的所有产品

mysql> select prod_name from products where prod_name regexp '^[0-9\\.]' order by prod_name;

+--------------+

| prod_name    |

+--------------+

| .5 ton anvil |

| 1 ton anvil  |

| 2 ton anvil  |

+--------------+

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 207,113评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,644评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,340评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,449评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,445评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,166评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,442评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,105评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,601评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,066评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,161评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,792评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,351评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,352评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,584评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,618评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,916评论 2 344