1.location表格创建
use class;
create table location (Region char(20),store_name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');
2.store_info表格创建
create table store_info (store_name char(20),sales int(10),Date char(10));
insert into store_info values('Los Angeles','1500','2020-12-05');
insert into store_info values('Houston','250','2020-12-07');
insert into store_info values('Los Angeles','300','2020-12-08');
insert into store_info values('Boston','700','2020-12-08');
select "字段" from "表名";
例:select store_name from store_info;
例:mysql> select store_name from store_info;
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Los Angeles |
| Boston |
+-------------+
4 rows in set (0.00 sec)
select distinct "字段" from "表名";
例:select distinct store_name from store_info;
例:mysql> select distinct store_name from store_info;
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.00 sec)
select distinct store_name "字段" from "表名";
例:select distinct store_name from store_info;
例:mysql> select distinct store_name from store_info;
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.00 sec)
select "字段" from "表名" where "条件1" {[and|or] "条件2"} + ;
例:select store_name from store_info where sales > 1000 or ( sales < 500and sales > 200 );
例:mysql> select store_name from store_info where sales > 1000;
+-------------+
| store_name |
+-------------+
| Los Angeles |
+-------------+
1 row in set (0.00 sec)
select "字段" from "表名" where "字段" in ('值1', '值2', ...);
例:select * from store_info where store_name in ('Los Angeles', 'Houston');
例:mysql> select * from store_info where store_name in ('Los Angeles','Hoston');
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Los Angeles | 300 | 2020-12-08 |
+-------------+-------+------------+
2 rows in set (0.00 sec)
select "字段" from "表名" where "字段" between '值1' and '值2';
例:select * from store_info where Date between '2020-12-06' and '2020-12-10';
例:mysql> select * from store_info where date between '2020-12-06'
and '2020-12-10';
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Houston | 250 | 2020-12-07 |
| Los Angeles | 300 | 2020-12-08 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
3 rows in set (0.00 sec)
名称 | 作用 | 举例 |
---|---|---|
‘A_Z’ | 所有以 ‘A’ 起头,另一个任何值的字符,且以 ‘Z’ 为结尾的字符串 | ‘ABZ’ 和 'A2Z’符合而 ‘AKKZ’ 不符合 |
‘ABC%’ | 所有以 ‘ABC’ 起头的字符串 | ‘ABCD’ 和 ‘ABCABC’ |
‘%XYZ’ | 所有以 ‘XYZ’ 结尾的字符串 | ‘WXYZ’ 和 ‘ZZXYZ’ |
‘%AN%’ | 所有含有 'AN’这个模式的字符串 | ‘LOS ANGELES’ 和 ‘SAN FRANCISCO’ |
‘_AN%’ | 所有第二个字母为 ‘A’ 和第三个字母为 ‘N’ 的字符串 | ‘SAN FRANCISCO’ 符合而 ‘LOS ANGELES’ 不符合 |
select "字段" from "表名" where "字段" like {模式};
例:select * from store_info where store_name like ‘%os%’;
例:mysql> select * from store_info where store_name like '%os%';
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Los Angeles | 300 | 2020-12-08 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
3 rows in set (0.00 sec)
select "字段" from "表名" [where “条件”] order by "字段" [ASC, DESC]; #ASC默认按照升序排序的;DESC是按降序排序的
例:select store_name,sales,Date from store_info order by sales DESC;
例:mysql> select store_name,sales,date from store_info order by sales DESC;
+-------------+-------+------------+
| store_name | sales | date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Boston | 700 | 2020-12-08 |
| Los Angeles | 300 | 2020-12-08 |
| Houston | 250 | 2020-12-07 |
+-------------+-------+------------+
4 rows in set (0.00 sec)
关键字 | 含义 |
---|---|
abs(x) |
返回 x 的绝对值 |
rand() |
返回 0 到 1 的随机数 |
mod(x,y) |
返回 x 除以 y 以后的余数 |
power(x,y) |
返回 x 的 y 次方 |
round(x) |
返回离 x 最近的整数 |
round(x,y) |
保留 x 的 y 位小数四舍五入后的值 |
sqrt(x) |
返回 x 的平方根 |
truncate(x,y) |
返回数字 x 截断为 y 位小数的值 |
ceil(x) |
返回大于或等于 x 的最小整数 |
floor(x) |
返回小于或等于 x 的最大整数 |
greatest(x1,x2...) |
返回集合中最大的值,也可以返回多个字段的最大的值 |
least(x1,x2…) |
返回集合中最小的值,也可以返回多个字段的最小的值 |
select abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
例:mysql> select abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
+---------+---------------------+----------+------------+-------------+
| abs(-1) | rand() | mod(5,3) | power(2,3) | round(1.89) |
+---------+---------------------+----------+------------+-------------+
| 1 | 0.04296933304795002 | 2 | 8 | 2 |
+---------+---------------------+----------+------------+-------------+
1 row in set (0.00 sec)
select round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);
例:mysql> select round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1),least(1.89,3,6.1,2.1);
+-----------------+-------------------+-----------+------------+-----------------------+
| round(1.8937,3) | truncate(1.235,2) | ceil(5.2) | floor(2.1) | least(1.89,3,6.1,2.1) |
+-----------------+-------------------+-----------+------------+-----------------------+
| 1.894 | 1.23 | 6 | 2 | 1.89 |
+-----------------+-------------------+-----------+------------+-----------------------+
1 row in set (0.00 sec)
关键字 | 含义 |
---|---|
avg() |
返回指定列的平均值 |
count() |
返回指定列中非 NULL 值的个数 |
min() |
返回指定列的最小值 |
max() |
返回指定列的最大值 |
sum(x) |
返回指定列的所有值之和 |
select avg(sales) from store_info;
例:mysql> select avg(sales) from store_info;
+------------+
| avg(sales) |
+------------+
| 687.5000 |
+------------+
1 row in set (0.00 sec)
select count(store_name) from store_info;
例:mysql> select count(store_name) from store_info;
+-------------------+
| count(store_name) |
+-------------------+
| 4 |
+-------------------+
1 row in set (0.00 sec)
select count(distinct store_name) from store_info;
例:mysql> select count(distinct store_name) from store_info;
+----------------------------+
| count(distinct store_name) |
+----------------------------+
| 3 |
+----------------------------+
1 row in set (0.00 sec)
select max(sales) from store_info;
例:mysql> select max(sales) from store_info;
+------------+
| max(sales) |
+------------+
| 1500 |
+------------+
1 row in set (0.00 sec)
select min(sales) from store_info;
例:mysql> select min(sales) from store_info;
+------------+
| min(sales) |
+------------+
| 250 |
+------------+
1 row in set (0.00 sec)
select sum(sales) from store_info;
例:mysql> select sum(sales) from store_info;
+------------+
| sum(sales) |
+------------+
| 2750 |
+------------+
1 row in set (0.00 sec)
关键字 | 含义 |
---|---|
trim() |
返回去除指定格式的值 |
concat(x,y) |
将提供的参数 x 和 y 拼接成一个字符串 |
substr(x,y) |
获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同 |
substr(x,y,z) |
获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串 |
length(x) |
返回字符串 x 的长度 |
replace(x,y,z) |
将字符串 z 替代字符串 x 中的字符串 y |
upper(x) |
将字符串 x 的所有字母变成大写字母 |
lower(x) |
将字符串 x 的所有字母变成小写字母 |
left(x,y) |
返回字符串 x 的前 y 个字符 |
right(x,y) |
返回字符串 x 的后 y 个字符 |
repeat(x,y) |
将字符串 x 重复 y 次 |
``space(x)` | 返回 x 个空格 |
strcmp(x,y) |
比较 x 和 y,返回的值可以为-1,0,1 |
reverse(x) |
将字符串 x 反转 |
字段拼接
select concat(Region, store_name) from localtion where store_name = 'Boston';
例:mysql> select concat(Region, store_name) from location where store_name = 'Boston';
+----------------------------+
| concat(Region, store_name) |
+----------------------------+
| EastBoston |
+----------------------------+
1 row in set (0.01 sec)
select Region || ' ' || store_name from localtion where store_name = 'Boston';
例:mysql> select Region || ' ' || store_name from location where store_name = 'Boston';
+-----------------------------+
| Region || ' ' || store_name |
+-----------------------------+
| East Boston |
+-----------------------------+
1 row in set (0.00 sec)
select substr(store_name,3) from location where store_name = 'Los Angeles';
例:mysql> select substr(store_name,3) from location where store_name = 'Los Angeles';
+----------------------+
| substr(store_name,3) |
+----------------------+
| s Angeles |
+----------------------+
1 row in set (0.00 sec)
select substr(store_name,2,4) from location where store_name = 'New York';
例:mysql> select substr(store_name,2,4) from location where store_name = 'New York';
+------------------------+
| substr(store_name,2,4) |
+------------------------+
| ew Y |
+------------------------+
1 row in set (0.00 sec)
字段移除
select trim ([ [位置] [要移除的字符串] from ] 字符串);
例:select trim(leading 'Ne ' from 'New York');
例:mysql> select TRIM(LEADinG 'Ne' from 'New York');
+------------------------------------+
| TRIM(LEADinG 'Ne' from 'New York') |
+------------------------------------+
| w York |
+------------------------------------+
1 row in set (0.00 sec)
select Region,length(store_name) from location;
例:mysql> select Region,length(store_name) from location;
+--------+--------------------+
| Region | length(store_name) |
+--------+--------------------+
| East | 6 |
| East | 8 |
| West | 11 |
| West | 7 |
+--------+--------------------+
4 rows in set (0.00 sec)
select REPLACE(Region,'ast','astern')from location;
例:mysql> select REPLACE(Region,'ast','astern')from location;
+--------------------------------+
| REPLACE(Region,'ast','astern') |
+--------------------------------+
| Eastern |
| Eastern |
| West |
| West |
+--------------------------------+
4 rows in set (0.01 sec)
select "字段1", SUM("字段2") from "表名" GROUP by "字段1";
例:select store_name,sum(sales) from store_info group by store_name order by sales desc;
例:mysql> select store_name, SUM(sales) from store_info GROUP by store_name order by sales desc;
+-------------+------------+
| store_name | SUM(sales) |
+-------------+------------+
| Los Angeles | 1800 |
| Boston | 700 |
| Houston | 250 |
+-------------+------------+
3 rows in set (0.00 sec)
select "字段1", SUM("字段2") from "表格名" GROUP by "字段1" HAVinG (函数条件);
例:select store_name, SUM(sales) from store_info group up store_name having SUM(sales) > 1500;
例:mysql> select store_name, SUM(sales) from store_info GROUP by store_name HAVinG SUM(sales) > 1500;
+-------------+------------+
| store_name | SUM(sales) |
+-------------+------------+
| Los Angeles | 1800 |
+-------------+------------+
1 row in set (0.00 sec)
select "表格別名"."字段1" [AS] "字段別名" from "表格名" [AS] "表格別名";
例:select A.store_name Store, SUM(A.sales) "Total sales" from store_info A group by A.store_name;
例:mysql> select A.store_name store, SUM(A.sales) "Total sales" from store_info A GROUP by A.store_name;
+-------------+-------------+
| store | Total sales |
+-------------+-------------+
| Boston | 700 |
| Houston | 250 |
| Los Angeles | 1800 |
+-------------+-------------+
3 rows in set (0.00 sec)
select "字段1" from "表格1" where "字段2" [比较运算符] #外查询
(select "字段1" from "表格2" where "条件"); #内查询
select * from location where store_name in (select store_name from store_info);
例:mysql> select * from location where store_name in (select store_name from store_info);
+--------+-------------+
| Region | Store_Name |
+--------+-------------+
| West | Los Angeles |
| West | Houston |
| East | Boston |
+--------+-------------+
3 rows in set (0.00 sec)
select sum(sales) from store_info where store_name in (select store_name from location where region = 'West');
例:mysql> select SUM(sales) from store_info where store_name in
-> (select store_name from location where Region = 'West');
+------------+
| SUM(sales) |
+------------+
| 2050 |
+------------+
1 row in set (0.00 sec)
select sum(A.sales) from store_info A where A.store_name in
(select store_name from location B where B.store_name = A.store_name);
例:mysql> select SUM(A.sales) from store_info A where A.store_name in
-> (select store_name from location B where B.store_name = A.store_name);
+--------------+
| SUM(A.sales) |
+--------------+
| 2750 |
+--------------+
1 row in set (0.00 sec)
select "字段1" from "表格1" where EXISTS (select * from "表格2" where "条件");
例:select SUM(sales) from store_info where exists (select * from location where Region = 'West');
例:mysql> select SUM(sales) from store_info where EXISTS (select * from location where Region = 'West');
+------------+
| SUM(sales) |
+------------+
| 2750 |
+------------+
1 row in set (0.00 sec)
1.inner join(内连接)
只返回两个表中联结字段相等的行
示例一
select * from location A inner join store_info B on A.store_name = B.store_name ;
例:mysql> select * from location A inner join store_info B on A.store_name = B.store_name ;
+--------+-------------+-------------+-------+------------+
| Region | Store_Name | Store_Name | Sales | Date |
+--------+-------------+-------------+-------+------------+
| West | Los Angeles | Los Angeles | 1500 | 2020-12-05 |
| West | Houston | Houston | 250 | 2020-12-07 |
| East | Boston | Boston | 700 | 2020-12-08 |
+--------+-------------+-------------+-------+------------+
3 rows in set (0.00 sec)
select * from location A, store_info B where A.store_name = B.store_name;
例:mysql> select * from location A, store_info B where A.store_name = B.store_name;
+--------+-------------+-------------+-------+------------+
| Region | Store_Name | Store_Name | Sales | Date |
+--------+-------------+-------------+-------+------------+
| West | Los Angeles | Los Angeles | 1500 | 2020-12-05 |
| West | Houston | Houston | 250 | 2020-12-07 |
| East | Boston | Boston | 700 | 2020-12-08 |
+--------+-------------+-------------+-------+------------+
3 rows in set (0.00 sec)
select A.region region, sum(B.sales) sales from location A, store_info B
where A.store_name = B.store_name group by region;
例:mysql> select A.region region, sum(B.sales) sales from location A, store_info B
-> where A.store_name = B.store_name group by region;
+--------+-------+
| region | sales |
+--------+-------+
| East | 700 |
| West | 1750 |
+--------+-------+
2 rows in set (0.00 sec)
2.left join(左连接)
select * from location A left join store_info B on A.store_name = B.store_name ;
例:mysql> select * from location A left join store_info B on A.store_name = B.store_name ;
+--------+-------------+-------------+-------+------------+
| Region | Store_Name | Store_Name | Sales | Date |
+--------+-------------+-------------+-------+------------+
| West | Los Angeles | Los Angeles | 1500 | 2020-12-05 |
| West | Houston | Houston | 250 | 2020-12-07 |
| East | Boston | Boston | 700 | 2020-12-08 |
| East | New York | NULL | NULL | NULL |
+--------+-------------+-------------+-------+------------+
4 rows in set (0.00 sec)
3.right join(右连接)
select * from location A right join store_info B on A.store_name = B.store_name ;
例:mysql> select * from location A right join store_info B on A.store_name = B.store_name ;
+--------+-------------+-------------+-------+------------+
| Region | Store_Name | Store_Name | Sales | Date |
+--------+-------------+-------------+-------+------------+
| East | Boston | Boston | 700 | 2020-12-08 |
| West | Los Angeles | Los Angeles | 1500 | 2020-12-05 |
| West | Houston | Houston | 250 | 2020-12-07 |
| NULL | NULL | Washington | 300 | 2020-12-08 |
+--------+-------------+-------------+-------+------------+
4 rows in set (0.00 sec)
4.create view (视图虚拟表或存储查询)
create view "视图表名" as "select 语句";
例:create view V_region_sales as select A.region region,sum(B.sales) sales from location A inner join store_info B on A.store_name = B.store_name group by region;
select * from V_region_sales;
drop view V_region_sales;
例:mysql> create view V_region_sales as select A.region region,sum(B.sales)sales from location A
-> inner join store_info B on A.store_name = B.store_name group by region;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from V_region_sales;
+--------+-------+
| region | sales |
+--------+-------+
| East | 700 |
| West | 1750 |
+--------+-------+
2 rows in set (0.00 sec)
mysql> drop view V_region_sales;
Query OK, 0 rows affected (0.00 sec)
5.union(联集)
① union 用法
[select 语句 1] union [select 语句 2];
select store_name from location union select store_name from store_info;
例:mysql> select store_name from location union select store_name from store_info;
+-------------+
| store_name |
+-------------+
| Boston |
| New York |
| Los Angeles |
| Houston |
| Washington |
+-------------+
5 rows in set (0.00 sec)
② union all 用法
[select 语句 1] union all [select 语句 2];
select store_name from location union all select store_name from store_info;
例:mysql> select store_name from location union all select store_name from store_info;
+-------------+
| store_name |
+-------------+
| Boston |
| New York |
| Los Angeles |
| Houston |
| Los Angeles |
| Houston |
| Washington |
| Boston |
+-------------+
8 rows in set (0.00 sec)
6.交集值
select A.store_name from location A inner join store_info B on A.store_name = B.store_name;
例:mysql> select A.store_name from location A inner join store_info B on A.store_name = B.store_name;
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.00 sec)
select A.store_name from location A inner join store_info B using(store_name);
例:mysql> select A.store_name from location A inner join store_info B using(store_name);
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.00 sec)
select DISTINCT A.store_name from location A inner join store_info B using(store_name);
select DISTINCT store_name from location where (store_name) IN (select store_name from store_info);
select DISTINCT A.store_name from location A left join store_info B using(store_name) where B.store_name IS NOT NULL;
select A.store_name from (select B.store_name from location B inner join store_info C on B.store_name = C.store_name) A
group by A.store_name;
例:mysql> select distinct A.store_name from location A inner join store_info B using(store_name);
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.01 sec)
mysql> select distinct store_name from location where (store_name) in (select store_name from store_info);
+-------------+
| store_name |
+-------------+
| Los Angeles |
| Houston |
| Boston |
+-------------+
3 rows in set (0.00 sec)
mysql> select distinct A.store_name from location A left join store_info B using(store_name)where B.store_name is not null;
+-------------+
| store_name |
+-------------+
| Boston |
| Los Angeles |
| Houston |
+-------------+
3 rows in set (0.01 sec)
mysql> select A.store_name from (select B.store_name from location B inner join store_info C on B.store_name = C.store_name)A
-> group by A.store_name;
+-------------+
| store_name |
+-------------+
| Boston |
| Houston |
| Los Angeles |
+-------------+
3 rows in set (0.00 sec)
mysql> select A.store_name from
-> (select distinct store_name from location union all select distinct store_name from store_info)A
-> group by A.store_name having count(*) > 1;
+-------------+
| store_name |
+-------------+
| Boston |
| Houston |
| Los Angeles |
+-------------+
3 rows in set (0.01 sec)
7.无交集值
select DISTINCT store_name from location where (store_name) NOT IN (select store_name from store_info);
例:mysql> select distinct store_name from location where (store_name) not in (select store_name from store_info);
+------------+
| store_name |
+------------+
| New York |
+------------+
1 row in set (0.00 sec)
select DISTINCT A.store_name from location A left join store_info B using(store_name) where B.store_name IS NULL;
例:mysql> select distinct A.store_name from location A left join store_info B using(store_name) where B.store_name is null;
+------------+
| store_name |
+------------+
| New York |
+------------+
1 row in set (0.00 sec)
select A.store_name from
(select DISTINCT store_name from location union all select DISTINCT store_name from store_info) A
group by A.store_name HAVING COUNT(*) = 1;
例:mysql> select A.store_name from
-> (select distinct store_name from location union all select distinct store_name from store_info)A
-> group by A.store_name having count(*) = 1;
+------------+
| store_name |
+------------+
| New York |
| Washington |
+------------+
2 rows in set (0.00 sec)
8.Case
select Case ("字段名")
when "条件1" then "结果1" # "条件" 可以是一个数值或是公式。 ELSE 子句则并不是必须的。
when "条件2" then "结果2"
...
[ELSE "结果N"]
end
from "表名";
例:select store_name, Case store_name
when 'Los Angeles' then sales * 2
when 'Boston' then 2000
ELSE sales
end
"New sales",Date #"New sales" 是用于 Case 那个字段的字段名。
from store_info;
例:mysql> select store_name, Case store_name
-> WHEN 'Los Angeles' THEN sales * 2
-> WHEN 'Boston' THEN 2000
-> ELSE sales
-> END
-> "New sales",Date
-> from store_info;
+-------------+-----------+------------+
| store_name | New sales | Date |
+-------------+-----------+------------+
| Los Angeles | 3000 | 2020-12-05 |
| Houston | 250 | 2020-12-07 |
| Washington | 300 | 2020-12-08 |
| Boston | 2000 | 2020-12-08 |
+-------------+-----------+------------+
4 rows in set (0.00 sec)
9.空值(NULL) 和 无值(‘’) 的区别
select length(NULL), length(''), length('1');
select * from City where name IS NULL;
select * from City where name IS NOT NULL;
select * from City where name = '';
select * from City where name <> '';
select COUNT(*) from City;c
select COUNT(name) from City;
例:mysql> SELECT length(NULL), length(''), length('1');
+--------------+------------+-------------+
| length(NULL) | length('') | length('1') |
+--------------+------------+-------------+
| NULL | 0 | 1 |
+--------------+------------+-------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM city WHERE name IS NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM city WHERE name IS NOT NULL;
+----------+
| name |
+----------+
| beijing |
| nanjing |
| shanghai |
| |
| |
+----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM city WHERE name = '';
Empty set (0.00 sec)
mysql> SELECT * FROM city WHERE name <> '';
+----------+
| name |
+----------+
| beijing |
| nanjing |
| shanghai |
| |
| |
+----------+
5 rows in set (0.00 sec)
mysql> SELECT COUNT(*) FROM city;
+----------+
| COUNT(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(name) FROM city;
+-------------+
| COUNT(name) |
+-------------+
| 5 |
+-------------+
1 row in set (0.00 sec)
10.正则表达式
匹配模式 | 描述 | 实例 |
---|---|---|
^ | 匹配文本的开始字符 | ‘^bd’ 匹配以 bd 开头的字符串 |
$ | 匹配文本的结束字符 | ‘qn$’ 匹配以 qn 结尾的字符串 |
. | 匹配任何单个字符 | ‘s.t’ 匹配任何 s 和 t 之间有一个字符的字符串 |
* | 匹配零个或多个在它前面的字符 | ‘fo*t’ 匹配 t 前面有任意个 o |
+ | 匹配前面的字符 1 次或多次 | ‘hom+’ 匹配以 ho 开头,后面至少一个m 的字符串 |
字符串 | 匹配包含指定的字符串 | ‘clo’ 匹配含有 clo 的字符串 |
p1|p2 | 匹配 p1 或 p2 | ‘bg|fg’ 匹配 bg 或者 fg |
[…] | 匹配字符集合中的任意一个字符 | ‘[abc]’ 匹配 a 或者 b 或者 c |
[^…] | 匹配不在括号中的任何字符 | ‘[ ^ ab]’ 匹配不包含 a 或者 b 的字符串 |
{n} | 匹配前面的字符串 n 次 | ‘g{2}’ 匹配含有 2 个 g 的字符串 |
{n,m} | 匹配前面的字符串至少 n 次 | 至多m 次 ‘f{1,3}’ 匹配 f 最少 1 次,最多 3 次 |
select "字段" from "表名" where "字段" REGEXP {模式};
例:select * from store_info where store_name REGEXP 'os';
select * from store_info where store_name REGEXP '^[A-G]';
select * from store_info where store_name REGEXP 'Ho|Bo';
例:mysql> SELECT * FROM store_info WHERE Store_Name REGEXP 'os';
+-------------+-------+------------+
| Store_Name | Sales | Date |
+-------------+-------+------------+
| Los Angeles | 1500 | 2020-12-05 |
| Boston | 700 | 2020-12-08 |
+-------------+-------+------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM store_info WHERE Store_Name REGEXP '^[A-G]';
+------------+-------+------------+
| Store_Name | Sales | Date |
+------------+-------+------------+
| Boston | 700 | 2020-12-08 |
+------------+-------+------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM store_info WHERE Store_Name REGEXP 'Ho|Bo';
+------------+-------+------------+
| Store_Name | Sales | Date |
+------------+-------+------------+
| Houston | 250 | 2020-12-07 |
| Boston | 700 | 2020-12-08 |
+------------+-------+------------+
2 rows in set (0.00 sec)