MySQL-查询语句(2)

举报
大虾好吃吗 发表于 2023/07/20 14:57:36 2023/07/20
【摘要】 聚合函数查询函数作用AVG()返回某列的平均值COUNT()返回某列的行数MAX()返回某列的最大值MIN()返回某列的最小值SUM()返回某列的和 count()函数​ Count()函数统计数据表中包含的记录行的总数,或者根据查询结果返回列中包含的数据行数。 Count(*)计算表中总的函数,不管某列有数列或者为空值 Count(字段名)计算指定列下总的行数,计算时将忽略空值的行mys...

聚合函数查询

函数 作用
AVG() 返回某列的平均值
COUNT() 返回某列的行数
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列的和

count()函数

​ Count()函数统计数据表中包含的记录行的总数,或者根据查询结果返回列中包含的数据行数。 Count(*)计算表中总的函数,不管某列有数列或者为空值 Count(字段名)计算指定列下总的行数,计算时将忽略空值的行

mysql> select count(*) from customers;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.01 sec)
mysql> select count(c_email) from customers;
+----------------+
| count(c_email) |
+----------------+
|              3 |
+----------------+
1 row in set (0.00 sec)
mysql> select o_num,count(f_id) from orderitems group by o_num;
+-------+-------------+
| o_num | count(f_id) |
+-------+-------------+
| 30001 |           4 |
| 30002 |           1 |
| 30003 |           1 |
| 30004 |           1 |
| 30005 |           4 |
+-------+-------------+
5 rows in set (0.00 sec)

sum()函数

sum()是一个求总和的函数,返回指定列值得总和。

mysql> select sum(quantity) from orderitems where o_num = 30005;
+---------------+
| sum(quantity) |
+---------------+
|            30 |
+---------------+
1 row in set (0.00 sec)
mysql> select o_num,sum(quantity)
    -> from orderitems
    -> group by o_num;
+-------+---------------+
| o_num | sum(quantity) |
+-------+---------------+
| 30001 |            33 |
| 30002 |             2 |
| 30003 |           100 |
| 30004 |            50 |
| 30005 |            30 |
+-------+---------------+
5 rows in set (0.01 sec)

​ 注意:sum()函数在计算时,忽略列值为NULL的行。

avg()函数

​ avg()函数通过计算返回的行数和每一行数据的和,求得指定列数据的平均值。

mysql> select avg(f_price)
    -> from fruits
    -> where s_id=103;
+--------------+
| avg(f_price) |
+--------------+
|     5.700000 |
+--------------+
1 row in set (0.02 sec)
mysql> select s_id,avg(f_price)
    -> from fruits
    -> group by s_id;
+------+--------------+
| s_id | avg(f_price) |
+------+--------------+
|  101 |     6.200000 |
|  102 |     8.933333 |
|  103 |     5.700000 |
|  104 |     7.000000 |
|  105 |     7.466667 |
|  106 |    15.700000 |
|  107 |     3.600000 |
+------+--------------+
7 rows in set (0.01 sec)

max()函数

​ max()返回指定列中的最大值

mysql> select max(f_price) from fruits;
+--------------+
| max(f_price) |
+--------------+
|        15.70 |
+--------------+
1 row in set (0.00 sec)
mysql> select s_id,max(f_price) 
    -> from fruits
    -> group by s_id;
+------+--------------+
| s_id | max(f_price) |
+------+--------------+
|  101 |        10.20 |
|  102 |        11.20 |
|  103 |         9.20 |
|  104 |         7.60 |
|  105 |        11.60 |
|  106 |        15.70 |
|  107 |         3.60 |
+------+--------------+
7 rows in set (0.01 sec)

min()函数

min()返回查询列中的最小值

mysql> select min(f_price) from fruits;
+--------------+
| min(f_price) |
+--------------+
|         2.20 |
+--------------+
1 row in set (0.00 sec)
mysql> select s_id,min(f_price) from fruits group by s_id;
+------+--------------+
| s_id | min(f_price) |
+------+--------------+
|  101 |         3.20 |
|  102 |         5.30 |
|  103 |         2.20 |
|  104 |         6.40 |
|  105 |         2.60 |
|  106 |        15.70 |
|  107 |         3.60 |
+------+--------------+
7 rows in set (0.00 sec)

连接查询

​ 连接是关系数据库模型的主要特点。连接查询是关系数据库中最主要的查询,主要包括内连接、外连接。通过连接运算符可以实现多个表查询。在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当查询数据时,通过连接操作查询出存放在多个表中的不同实体的信息。当两个或多个表现中存在相同意义的字段时,便可以通过这些字段对不同的表进行连接查询。

内连接查询

​ 内连接(inner join)使用比较运算符进行表间某些列数据的比较操作,并列出这些表中与连接条件相匹配的数据行,组合成新纪录,也就是说,在内连接查询中,只有满足条件的记录才能出现在结果关系中。

​ 创建表并插入数据

mysql> create table suppliers
 -> (
 -> s_id int not null auto_increment,
 -> s_name char(50) not null,
 -> s_city char(50) null,
 -> s_zip char(10) null,
 -> s_call char(50) not null,
 -> primary key(s_id) 
 -> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into suppliers(s_id,s_name,s_city,s_zip,s_call)
 -> values(101,'FastFruit Inc.','tianjin','300000','48075'),
 -> (102,'LT Supplies','chongqing','400000','44333'),
 -> (103,'acme','shanghai','200000','90046'),
 -> (104,'fnk inc.','zhongshan','528437','11111'),
 -> (105,'good set','taiyuang','030000','22222'),
 -> (106,'just eat ours','beijing','010','45678'),
 -> (107,'dk inc.','zhengzhou','450000','33332');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select suppliers.s_id,s_name,f_name,f_price
    -> from fruits,suppliers
    -> where fruits.s_id = suppliers.s_id;
+------+----------------+------------+---------+
| s_id | s_name         | f_name     | f_price |
+------+----------------+------------+---------+
|  101 | FastFruit Inc. | apple      |    5.20 |
|  103 | acme           | apricot    |    2.20 |
|  101 | FastFruit Inc. | blackberry |   10.20 |
|  104 | fnk inc.       | berry      |    7.60 |
|  107 | dk inc.        | xxxx       |    3.60 |
|  102 | LT Supplies    | orange     |   11.20 |
|  105 | good set       | melon      |    8.20 |
|  101 | FastFruit Inc. | cherry     |    3.20 |
|  104 | fnk inc.       | lemon      |    6.40 |
|  106 | just eat ours  | mango      |   15.70 |
|  105 | good set       | xbabay     |    2.60 |
|  105 | good set       | xxtt       |   11.60 |
|  103 | acme           | coconut    |    9.20 |
|  102 | LT Supplies    | banana     |   10.30 |
|  102 | LT Supplies    | grape      |    5.30 |
|  107 | dk inc.        | xbababa    |    3.60 |
+------+----------------+------------+---------+
16 rows in set (0.04 sec)

​ 如果在一个连接查询中,涉及的两个表都是同一个表,这种查询称为自连接查询。自连接是一种特殊的内连接,它是指相互连接的表在物理上为同一张表,但可以在逻辑上分为两张表。

mysql> select f1.f_id,f1.f_name
    -> from fruits as f1, fruits as f2
    -> where f1.s_id = f2.s_id and f2.f_id = 'a1';
+------+------------+
| f_id | f_name     |
+------+------------+
| a1   | apple      |
| b1   | blackberry |
| c0   | cherry     |
+------+------------+
3 rows in set (0.00 sec)

外连接查询

​ 外连接查询将将查询多个表中相关联的行,内连接时,返回查询结果集合中的仅是符合查询条件和连接条件的行。但有时候需要包含没有关联的行中数据,即返回查询结果集合中的不仅包含符合连接条件的行,而且还包含左表(左外连接或左连接)、右表(右外连接或右连接)或两个连接表(全外连接)中的所有数据行。外连接分为左外连接或左连接和右外连接或右连接。

​ Left join(左连接):返回包括左表中的所有记录和右表中连接字段相等的记录。

​ Right join(右连接):返回包括右表中的所有记录和左表中连接字段相等的记录。

mysql> create table orders
 -> (
 -> o_num int not null auto_increment,
 -> o_date datetime not null,
 -> c_id int not null,
 -> primary key(o_num)
 -> ) ;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into orders(o_num,o_date,c_id)
 -> values(30001,'2008-09-01',10001),
 -> (30002,'2008-09-12',10003),
 -> (30003,'2008-09-30',10004),
 -> (30004,'2008-10-03',10005),
 -> (30005,'2008-10-08',10001);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

1、左连接

mysql> select customers.c_id,orders.o_num
    -> from customers left outer join orders
    -> on
    -> customers.c_id = orders.c_id;
+-------+-------+
| c_id  | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10004 | 30003 |
| 10001 | 30005 |
| 10002 |  NULL |
+-------+-------+
5 rows in set (0.00 sec)

2、右连接

mysql> select customers.c_id,orders.o_num
    -> from customers right outer join orders
    -> on
    -> customers.c_id = orders.c_id;
+-------+-------+
| c_id  | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10004 | 30003 |
|  NULL | 30004 |
| 10001 | 30005 |
+-------+-------+
5 rows in set (0.00 sec)

复合条件连接查询

​ 复合条件连接查询是在连接查询的过程中,通过添加过滤条件,限制查询的结果,使查询的结果更加准确。

mysql> select customers.c_id,orders.o_num
    -> from customers inner join orders
    -> on
    -> customers.c_id = orders.c_id and customers.c_id = 10001;
+-------+-------+
| c_id  | o_num |
+-------+-------+
| 10001 | 30001 |
| 10001 | 30005 |
+-------+-------+
2 rows in set (0.00 sec)

mysql> select suppliers.s_id,s_name,f_name,f_price
    -> from fruits inner join suppliers
    -> on
    -> fruits.s_id = suppliers.s_id
    -> order by fruits.s_id;
+------+----------------+------------+---------+
| s_id | s_name         | f_name     | f_price |
+------+----------------+------------+---------+
|  101 | FastFruit Inc. | apple      |    5.20 |
|  101 | FastFruit Inc. | blackberry |   10.20 |
|  101 | FastFruit Inc. | cherry     |    3.20 |
|  102 | LT Supplies    | orange     |   11.20 |
|  102 | LT Supplies    | banana     |   10.30 |
|  102 | LT Supplies    | grape      |    5.30 |
|  103 | acme           | apricot    |    2.20 |
|  103 | acme           | coconut    |    9.20 |
|  104 | fnk inc.       | berry      |    7.60 |
|  104 | fnk inc.       | lemon      |    6.40 |
|  105 | good set       | melon      |    8.20 |
|  105 | good set       | xbabay     |    2.60 |
|  105 | good set       | xxtt       |   11.60 |
|  106 | just eat ours  | mango      |   15.70 |
|  107 | dk inc.        | xxxx       |    3.60 |
|  107 | dk inc.        | xbababa    |    3.60 |
+------+----------------+------------+---------+
16 rows in set (0.01 sec)

子查询

​ 子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从mysql4.1开始引入。在select子句中先计算子查询,子查询结果作为外层另一个查询的过滤条件,查询可以基于一个表或者多个表。

​ 子查询中常用的操作符有any(some)、all、in、exists。子查询可以添加到select、update和delete语句中,而且可以进行多层嵌套。子查询中也可以使用比较运算符,如“<”,“<=”,“>”,“>=”和“!=”等。

anysome关键字的子查询

​ any和some关键字是同义词,表示满足其中任一条件,它们允许创建一个表达式对子查询的返回值列进行比较,只要满足内层子查询中的任何一个比较条件,就返回一个结果作为外层查询的条件。

​ 创建两个表并插入数据

mysql> create table tb1(num1 int not null);
Query OK, 0 rows affected (0.01 sec)

mysql> create table tb2(num2 int not null);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tb1 values(1),(5),(13),(27);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into tb2 values(6),(14),(11),(20);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

​ 查看满足条件的行

mysql> select num1 from tb1 where num1 > any (select num2 from tb2);
+------+
| num1 |
+------+
|   13 |
|   27 |
+------+
2 rows in set (0.00 sec)

all关键字的子查询

​ all关键字与any和some不同,使用all时需要同时满足所有内层查询的条件。

mysql> select num1 from tb1 where num1 > all (select num2 from tb2);
+------+
| num1 |
+------+
|   27 |
+------+
1 row in set (0.00 sec)

exists关键字的子查询

​ exists关键字后面的参数是一个任意的子查询,系统对子查询进行运算以判断它是否返回行,如果至少返回一行,那么exists的结果为true,此时外层查询语句将进行查询;如果子查询没有返回任何行,那么exists返回的结果是false,此时外层语句将不进行查询。

mysql> select * from fruits where exists (select s_name from suppliers where s_id  = 107);
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    3.20 |
| l2   |  104 | lemon      |    6.40 |
| m1   |  106 | mango      |   15.70 |
| m2   |  105 | xbabay     |    2.60 |
| m3   |  105 | xxtt       |   11.60 |
| o2   |  103 | coconut    |    9.20 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)

in关键字的子查询

​ in关键字进行子查询时,内层查询语句仅仅返回一个数据列,这个数据列里的值将提供给外层查询语句进行比较操作。

mysql> select c_id from orders where o_num in
    -> (select o_num from orderitems where f_id = 'c0');
+-------+
| c_id  |
+-------+
| 10004 |
| 10001 |
+-------+
2 rows in set (0.00 sec)

带比较运算符的子查询

mysql> select s_id,f_name from fruits
 -> where s_id = 
 -> (select s1.s_id from suppliers as s1 where s1.s_city='tianjin');
+------+------------+
| s_id | f_name     |
+------+------------+
| 101 | apple       |
| 101 | blackberry  |
| 101 | cherry      |
+------+------------+
3 rows in set (0.00 sec)

合并查询结果

​ 利用union关键字,可以给出多条select语句,并将它们的结果组合成单个结果集。合并时,两个表对应的列数和数据类型必须相同。各个select语句之间使用union或union all关键字分隔。union不使用关键字all,执行的时候删除重复的记录,所有返回的行都是唯一的;使用关键字all的作用是不删除重复行也不对结果进行自动排序。

mysql> select s_id,f_name,f_price
    -> from fruits
    -> where f_price < 9.0
    -> union all
    -> select s_id,f_name,f_price
    -> from fruits
    -> where s_id in(101,103);
+------+------------+---------+
| s_id | f_name     | f_price |
+------+------------+---------+
|  101 | apple      |    5.20 |
|  103 | apricot    |    2.20 |
|  104 | berry      |    7.60 |
|  107 | xxxx       |    3.60 |
|  105 | melon      |    8.20 |
|  101 | cherry     |    3.20 |
|  104 | lemon      |    6.40 |
|  105 | xbabay     |    2.60 |
|  102 | grape      |    5.30 |
|  107 | xbababa    |    3.60 |
|  101 | apple      |    5.20 |
|  103 | apricot    |    2.20 |
|  101 | blackberry |   10.20 |
|  101 | cherry     |    3.20 |
|  103 | coconut    |    9.20 |
+------+------------+---------+
15 rows in set (0.00 sec)

​ union和union all的区别:使用union all的功能是不删除重复行,加上all关键字语句执行时所需要的资源少,所以尽可能地使用它,因此知道有重复行但是想保留这些行,确定查询结果中不会有重复数据或者不需要去掉重复数据的时候,应当使用union all以提高查询效率。

mysql> select s_id,f_name,f_price
    -> from fruits
    -> where f_price < 9.0
    -> union
    -> select s_id,f_name,f_price
    -> from fruits
    -> where s_id in(101,103);
+------+------------+---------+
| s_id | f_name     | f_price |
+------+------------+---------+
|  101 | apple      |    5.20 |
|  103 | apricot    |    2.20 |
|  104 | berry      |    7.60 |
|  107 | xxxx       |    3.60 |
|  105 | melon      |    8.20 |
|  101 | cherry     |    3.20 |
|  104 | lemon      |    6.40 |
|  105 | xbabay     |    2.60 |
|  102 | grape      |    5.30 |
|  107 | xbababa    |    3.60 |
|  101 | blackberry |   10.20 |
|  103 | coconut    |    9.20 |
+------+------------+---------+
12 rows in set (0.01 sec)

为表和字段取别名

​ 前面介绍了分组查询、聚合函数查询和嵌套子查询,取别名使用关键字as为查询结果中的某一列指定一个特别的名字。可以为字段或者表分别取别名,在查询时,使用别名替代指定的内容。

为表取别名

mysql> select * from orders as abc where abc.o_num = 30001;
+-------+---------------------+-------+
| o_num | o_date              | c_id  |
+-------+---------------------+-------+
| 30001 | 2008-09-01 00:00:00 | 10001 |
+-------+---------------------+-------+
1 row in set (0.00 sec)

为字段取别名

​ 先给fruits表指定别名为fa,f_name列名指定为fruits_name,f_price列名指定为fruits_price,并且价格小于8

mysql> select fa.f_name as fruits_name,fa.f_price as fruits_price
    -> from fruits as fa
    -> where fa.f_price < 8;
+-------------+--------------+
| fruits_name | fruits_price |
+-------------+--------------+
| apple       |         5.20 |
| apricot     |         2.20 |
| berry       |         7.60 |
| xxxx        |         3.60 |
| cherry      |         3.20 |
| lemon       |         6.40 |
| xbabay      |         2.60 |
| grape       |         5.30 |
| xbababa     |         3.60 |
+-------------+--------------+
9 rows in set (0.00 sec)

使用正则表达式查询

​ 正则表达式通常被用来检索或替换那些符合某个模式的文本内容,根据指定的匹配模式匹配文本中符合要求的特殊字符串。例如从一个文本文件中提取电话号码,查找一篇文章中重复的单词或者替换用户输入的某些敏感词语等等,这些地方都可以使用正则表达式。正则表达式强大且灵活,可以应用于非常复杂的查询。mysql中使用regexp关键字指定正则表达式的字符匹配模式。

选项 说明 例子 匹配值实例
^ 匹配文本的开始字符 ^b’匹配以字母b开头的字符串 book,big,bike
$ 匹配文本的结束字符 st$'匹配以st结尾的字符串 test,resist
. 匹配任何单个字符 b.t’匹配任何b和t之间有一个字符 bit,bat,but
* 匹配零个或者多个在它前面的字符 f*n’匹配字符n前面有任意个字符 fn,fan,faaan
+ 匹配前面的字符1次或多次 ba+'匹配以b开头后面紧跟至少有一个a ba,bay,bare,battle
<字符串> 匹配包含指定的字符串的文本 fa’ fan,afa,faad
[字符集合] 匹配字符集合中的任何一个字符 [xz]'匹配x或者z dizzy,zabra,extra
[^] 匹配不在括号中的任何字符 [^abc]'匹配任何不包括a、b或c的字符串 desk,fox,f8ke
字符串{n,} 匹配前面的字符串至少n次 b{2}匹配2个或者更多的b bbb,bbbbb
字符串{n,m} 匹配前面的字符串至少n次,至多m次。如果n为0,则省略 b{2,4}匹配最少2个,最多4个 bbb,bbb,bbbb

查询以特定字符或字符串开头的记录

mysql> select * from fruits where f_name regexp '^b';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| t1   |  102 | banana     |   10.30 |
+------+------+------------+---------+
3 rows in set (0.00 sec)

查询以特定字符或字符串结尾的记录

mysql> select * from fruits where f_name regexp 'y$';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| c0   |  101 | cherry     |    3.20 |
| m2   |  105 | xbabay     |    2.60 |
+------+------+------------+---------+
4 rows in set (0.05 sec)

用符合**‘.’**来代替字符串中的任意一个字符

mysql> select * from fruits where f_name regexp 'a.g';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1  |  102 | orange |   11.20 |
| m1   |  106 | mango  |   15.70 |
+------+------+--------+---------+
2 rows in set (0.00 sec)

使用**“*”“+”**来匹配多个字符

mysql>  select * from fruits where f_name regexp '^ba*';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| t1   |  102 | banana     |   10.30 |
+------+------+------------+---------+
3 rows in set (0.00 sec)
mysql> select * from fruits where f_name regexp '^ba+';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| t1   |  102 | banana |   10.30 |
+------+------+--------+---------+
1 row in set (0.00 sec)

匹配指定字符串

mysql> select * from fruits where f_name regexp 'on|ap';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| a1   |  101 | apple   |    5.20 |
| a2   |  103 | apricot |    2.20 |
| bs2  |  105 | melon   |    8.20 |
| l2   |  104 | lemon   |    6.40 |
| o2   |  103 | coconut |    9.20 |
| t2   |  102 | grape   |    5.30 |
+------+------+---------+---------+
6 rows in set (0.00 sec)
mysql> select * from fruits where f_name like 'on';
Empty set (0.00 sec)

匹配指定字符中的任意一个

mysql> select * from fruits where f_name regexp '[ot]';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| a2   |  103 | apricot |    2.20 |
| bs1  |  102 | orange  |   11.20 |
| bs2  |  105 | melon   |    8.20 |
| l2   |  104 | lemon   |    6.40 |
| m1   |  106 | mango   |   15.70 |
| m3   |  105 | xxtt    |   11.60 |
| o2   |  103 | coconut |    9.20 |
+------+------+---------+---------+
7 rows in set (0.00 sec)
mysql> select * from fruits where s_id regexp '[456]';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b2   |  104 | berry  |    7.60 |
| bs2  |  105 | melon  |    8.20 |
| l2   |  104 | lemon  |    6.40 |
| m1   |  106 | mango  |   15.70 |
| m2   |  105 | xbabay |    2.60 |
| m3   |  105 | xxtt   |   11.60 |
+------+------+--------+---------+
6 rows in set (0.00 sec)

匹配指定字符以外的字符

mysql> select * from fruits where f_id regexp '[^a-e1-2]';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| b5   |  107 | xxxx    |    3.60 |
| bs1  |  102 | orange  |   11.20 |
| bs2  |  105 | melon   |    8.20 |
| c0   |  101 | cherry  |    3.20 |
| l2   |  104 | lemon   |    6.40 |
| m1   |  106 | mango   |   15.70 |
| m2   |  105 | xbabay  |    2.60 |
| m3   |  105 | xxtt    |   11.60 |
| o2   |  103 | coconut |    9.20 |
| t1   |  102 | banana  |   10.30 |
| t2   |  102 | grape   |    5.30 |
| t4   |  107 | xbababa |    3.60 |
+------+------+---------+---------+
12 rows in set (0.00 sec)

使用**{n,}或者{n,m}**来指定字符串连续出现的次数

mysql> select * from fruits where f_name regexp 'x{2,}';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b5   |  107 | xxxx   |    3.60 |
| m3   |  105 | xxtt   |   11.60 |
+------+------+--------+---------+
2 rows in set (0.00 sec)
mysql> select * from fruits where f_name regexp 'ba{1,3}';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| m2   |  105 | xbabay  |    2.60 |
| t1   |  102 | banana  |   10.30 |
| t4   |  107 | xbababa |    3.60 |
+------+------+---------+---------+
3 rows in set (0.00 sec)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。