1. 使用ORDER BY语句来实现排序
2. 排序可针对一个或多个字段
3. ASC:升序默认排序方式
4. DESC:降序
5. ORDER BY的语法结构
SELECT column1, column2,... FROM table_name ORDER BY column1, column2,...
ASC|DESC;
6. 按单字段排序
7:按多字段排序
以此表为例,进行高级操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)使用 ORDER BY 进行字段查询
mysql> select name,age from a order by age desc;
+----------+------+
| name | age |
+----------+------+
| wuhan | 22 |
| zhucii | 20 |
| maliu | 20 |
| weipai | 19 |
| lisi | 19 |
| zhangsan | 18 |
| lisi | 18 |
| wangwu | 17 |
| liming | 16 |
+----------+------+
9 rows in set (0.00 sec)
mysql> select name,age from a order by age;
+----------+------+
| name | age |
+----------+------+
| liming | 16 |
| wangwu | 17 |
| zhangsan | 18 |
| lisi | 18 |
| weipai | 19 |
| lisi | 19 |
| zhucii | 20 |
| maliu | 20 |
| wuhan | 22 |
+----------+------+
9 rows in set (0.00 sec)
(2)使用 ORDER BY 进行多字段查询
mysql> select age,name,height from a order by age desc,height desc;
+------+----------+--------+
| age | name | height |
+------+----------+--------+
| 22 | wuhan | 190 |
| 20 | maliu | 180 | # maliu 和 zhucii 的age 一样,所以 height 进行了降序排序
| 20 | zhucii | 150 |
| 19 | weipai | 190 | # weipai 和 lisi 的 age 一样,所以 height 进行了降序排序
| 19 | lisi | 168 |
| 18 | lisi | 180 | # lisi 和 zhangsan 的 age 一样,所以 height 进行了降序排序
| 18 | zhangsan | 165 |
| 17 | wangwu | 170 |
| 16 | liming | 175 |
+------+----------+--------+
9 rows in set (0.00 sec)
mysql> select age,name,height from a order by age,height ;
+------+----------+--------+
| age | name | height |
+------+----------+--------+
| 16 | liming | 175 |
| 17 | wangwu | 170 |
| 18 | zhangsan | 165 | # zhangsan 和 lisi 的age 一样,所以 height 进行了升序排序
| 18 | lisi | 180 |
| 19 | lisi | 168 | # lisi 和 weipai 的 age 一样,所以 height 进行了升序排序
| 19 | weipai | 190 |
| 20 | zhucii | 150 | # zhucii 和 maliu 的 age 一样,所以 height 进行了升序排序
| 20 | maliu | 180 |
| 22 | wuhan | 190 |
+------+----------+--------+
9 rows in set (0.00 sec)
1:使用GROUP BY语句来实现分组
2:通常结合聚合函数一起使用
3:可以按一个或多个字段对结果进行分组
4:GROUP BY的语法结构
SELECT column_name,aggregate_function(column_name)FROM table_name
WHERE column_name operator value GROUP BY column_name;
以此表为例进行高级操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)GROUP BY分组示例
mysql> select count(name),age from a where weight >=140 group by age;
+-------------+------+
| count(name) | age |
+-------------+------+
| 1 | 17 |
| 1 | 18 |
| 1 | 19 |
| 1 | 20 |
| 1 | 22 |
+-------------+------+
5 rows in set (0.00 sec)
(2)GROUP BY结合ORDER BY
mysql> select count(name),age from a where weight >=120 group by age order by count(name) desc;
+-------------+------+
| count(name) | age |
+-------------+------+
| 2 | 18 |
| 2 | 19 |
| 1 | 17 |
| 1 | 20 |
| 1 | 22 |
| 1 | 16 |
+-------------+------+
6 rows in set (0.00 sec)
1:只返回SELECT查询结果的第一行或前几行
2:使用LIMIT语句限制条目
3:LIMIT语法结构
SELECT column1, column2,... FROM table_name LIMIT [offset,] number;
4:LIMIT限制结果条数
5:不从第一条开始取值
以此表进行限制条目操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)限制结果的行数
mysql> select * from a limit 5;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
+------+------+----------+--------+--------+
5 rows in set (0.00 sec)
(2)不从第一条开始取值
mysql> select * from a limit 1,5;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
+------+------+----------+--------+--------+
5 rows in set (0.00 sec)
1:使用AS语句设置别名,关键字AS可省略
2:设置别名时,保证不能与库中其他表或字段名称冲突
3:别名的语法结构
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s)FROM table_name AS alias_name;
4:AS的用法
mysql> select count(*) as number from player;
mysql> select p.id,p.name from player as p limit 1;
5:AS作为连接语句
mysql> create table tmp as select * from player;
Query Ok,3218 rows affected (0.15 sec)
Records: 3218 Duplicates: 0 Warnings: 0
以此表为例进行 AS 操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)AS 的第一种操作
mysql> select count(*) as num from a;
+-----+
| num |
+-----+
| 9 |
+-----+
1 row in set (0.00 sec)
(2)AS 的第二种操作
mysql> select id aaa,height bbb,weight ccc from a as aa;
+------+------+------+
| aaa | bbb | ccc |
+------+------+------+
| 1 | 175 | 130 |
| 2 | 165 | 160 |
| 3 | 170 | 140 |
| 4 | 180 | 120 |
| 5 | 190 | 180 |
| 6 | 150 | 90 |
| 7 | 180 | 140 |
| 8 | 190 | 170 |
| 9 | 168 | 130 |
+------+------+------+
9 rows in set (0.00 sec)
mysql> mysql> select id aaa,height bbb,weight ccc from a as aa where aa.height>=180;
+------+------+------+
| aaa | bbb | ccc |
+------+------+------+
| 4 | 180 | 120 |
| 5 | 190 | 180 |
| 7 | 180 | 140 |
| 8 | 190 | 170 |
+------+------+------+
4 rows in set (0.00 sec)
mysql> mysql> select id aaa,height bbb,weight ccc from a as aa where aa.height>=100 limit 3;
+------+------+------+
| aaa | bbb | ccc |
+------+------+------+
| 1 | 175 | 130 |
| 2 | 165 | 160 |
| 3 | 170 | 140 |
+------+------+------+
3 rows in set (0.00 sec)
(3)AS作为连接语句
mysql> create table a1 as select * from a;
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> desc a1;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | int(1) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| height | int(3) | YES | | NULL | |
| weight | int(3) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
9 rows in set (0.00 sec)
1:用于替换字符串中的部分字符
2:通常配合 LIKE 一起使用,并协同 WHERE 完成查询
3:常用通配符
4:通配符%的用法
mysql> select id,name,level from player where name like "%s";
5:通配符_的用法
mysql> select id,name,level from player where name like '_uess";
以此表进行通配符操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)使用 % 进行检索查询
mysql> select * from a where name like 'l%';
+------+------+--------+--------+--------+
| id | age | name | height | weight |
+------+------+--------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 4 | 18 | lisi | 180 | 120 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+--------+--------+--------+
3 rows in set (0.00 sec)
mysql> select * from a where name like '%n';
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 2 | 18 | zhangsan | 165 | 160 |
| 8 | 22 | wuhan | 190 | 170 |
+------+------+----------+--------+--------+
2 rows in set (0.00 sec)
(2)通配符_的用法
mysql> select * from a where name like 'li_i';
+------+------+------+--------+--------+
| id | age | name | height | weight |
+------+------+------+--------+--------+
| 4 | 18 | lisi | 180 | 120 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+------+--------+--------+
2 rows in set (0.00 sec)
mysql> select * from a where name like 'zhang_an';
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 2 | 18 | zhangsan | 165 | 160 |
+------+------+----------+--------+--------+
1 row in set (0.00 sec)
1:也称作内查询或者嵌套查询
2:先于主查询被执行,其结果将作为外层主查询的条件
3:在增删改查中都可以使用子查询
4:支持多层嵌套
5:IN语句是用来判断某个值是否在给定的结果集中
6:子查询的用法
mysq>select name,level from player where id in (select id from player where level>=45);
mysql> insert into tmp select * from player where id in (select id from player;
mysq>update tmp set level = level -7 where id in (select a.id from (select id from tmp where level >= 47) a);
mysq/>delete from tmp where id in (select a.id from(select id from tmp where level=47) a);
EXIST 关键字子查询
EXIST这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空,则返回 TRUE;反之,则返回 FALSE。例如,先通过子查询判断返回是否为 TRUE,如果用户shirley存在,则计算整个tmp表的总记录数量
mysq>select count(*) as number from tmp where EXISTS (select id from tmp where name='shirley);
以此表进行子查询
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
(1)查询列表信息
mysql> select name,height from a where height in (select height from a where height >=180);
+--------+--------+
| name | height |
+--------+--------+
| lisi | 180 |
| weipai | 190 |
| maliu | 180 |
| wuhan | 190 |
+--------+--------+
4 rows in set (0.00 sec)
mysql> select name,height from a where height in (select height from a where height );
+----------+--------+
| name | height |
+----------+--------+
| liming | 175 |
| zhangsan | 165 |
| wangwu | 170 |
| lisi | 180 |
| weipai | 190 |
| zhucii | 150 |
| maliu | 180 |
| wuhan | 190 |
| lisi | 168 |
+----------+--------+
9 rows in set (0.00 sec)
(2)插入数值
1.重新复制表 a 的表结构,为表 aa
mysql> create table aa like a;
Query OK, 0 rows affected (0.01 sec)
2.符合条件的数值从表 a 中 复制给 表 aa
mysql> insert into aa select * from a where weight in (select weight from a where weight >=140);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
3.验证效果
mysql> mysql> select * from aa;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 5 | 19 | weipai | 190 | 180 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
+------+------+----------+--------+--------+
5 rows in set (0.00 sec)
(3)定义别名根据条件查询
mysql> select b.name,b.height from (select name,height from a where height>=170) as b;
+--------+--------+
| name | height |
+--------+--------+
| liming | 175 |
| wangwu | 170 |
| lisi | 180 |
| weipai | 190 |
| maliu | 180 |
| wuhan | 190 |
+--------+--------+
6 rows in set (0.00 sec)
mysql> update a1 set height=height+10 where height in(select a.height from (select height from a where height>=180) as a);
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 190 | 120 |
| 5 | 19 | weipai | 200 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 190 | 140 |
| 8 | 22 | wuhan | 200 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
9 rows in set (0.00 sec)
(4)删除符合条件后删除
以 a1 这张表为例删除示例
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 200 | 120 |
| 5 | 19 | weipai | 200 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 200 | 140 |
| 8 | 22 | wuhan | 200 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
9 rows in set (0.00 sec)
mysql> delete from a1 where height in (select height from (select height from a1 where height >=180 and height<=200)a1);
Query OK, 4 rows affected (0.00 sec)
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 6 | 20 | zhucii | 150 | 90 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
5 rows in set (0.00 sec)
1:表示缺失的值
2:与数字0或者空白(spaces)是不同的
3:使用IS NULL或IS NOT NULL进行判断
4:NULL值和空值的区别
以这张表为例进行 NULL 值示例
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 6 | 20 | zhucii | 150 | 90 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
5 rows in set (0.00 sec)
(1)测试 NULL 值为空
mysql> insert into a1 values(default,default,default,default,default);
Query OK, 1 row affected (0.00 sec)
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 6 | 20 | zhucii | 150 | 90 |
| 9 | 19 | lisi | 168 | 130 |
| NULL | NULL | NULL | NULL | NULL |
+------+------+----------+--------+--------+
6 rows in set (0.00 sec)
mysql> select count(weight) from a1;
+---------------+
| count(weight) |
+---------------+
| 5 |
+---------------+
1 row in set (0.01 sec)
(2)测试 0 占位
mysql> insert into a1 values(10,20,'wangju',0,160);
Query OK, 1 row affected (0.00 sec)
mysql> select * from a1;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 6 | 20 | zhucii | 150 | 90 |
| 9 | 19 | lisi | 168 | 130 |
| NULL | NULL | NULL | NULL | NULL |
| 10 | 20 | wangju | 0 | 160 |
+------+------+----------+--------+--------+
7 rows in set (0.00 sec)
mysql> select count(height) from a1;
+---------------+
| count(height) |
+---------------+
| 6 |
+---------------+
1 row in set (0.00 sec)
(3)查询表中带有是 NULL 值的数据
mysql> select * from a1 where weight is null;
+------+------+------+--------+--------+
| id | age | name | height | weight |
+------+------+------+--------+--------+
| NULL | NULL | NULL | NULL | NULL |
+------+------+------+--------+--------+
1 row in set (0.00 sec)
(4)查询表中带有不是 NULL 值的数据
mysql> select * from a1 where weight is not null;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 6 | 20 | zhucii | 150 | 90 |
| 9 | 19 | lisi | 168 | 130 |
| 10 | 20 | wangju | 0 | 160 |
+------+------+----------+--------+--------+
6 rows in set (0.00 sec)
1:根据指定的匹配模式匹配记录中符合要求的特殊字符
2:使用REGEXP关键字指定匹配模式
3:常用匹配模式
以此表为例进行正则表达式操作
mysql> select * from a;
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
9 rows in set (0.00 sec)
(1)查询 name 段以 w 开头的数据
mysql> select * from a where name REGEXP '^w';
+------+------+--------+--------+--------+
| id | age | name | height | weight |
+------+------+--------+--------+--------+
| 3 | 17 | wangwu | 170 | 140 |
| 5 | 19 | weipai | 190 | 180 |
| 8 | 22 | wuhan | 190 | 170 |
+------+------+--------+--------+--------+
3 rows in set (0.00 sec)
(2)查询 name 段以 n 结尾的数据
mysql> select * from a where name REGEXP 'n$';
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 2 | 18 | zhangsan | 165 | 160 |
| 8 | 22 | wuhan | 190 | 170 |
+------+------+----------+--------+--------+
2 rows in set (0.00 sec)
(3)查询 name 段的任意字符
mysql> select * from a where name REGEXP '[a-z]';
+------+------+----------+--------+--------+
| id | age | name | height | weight |
+------+------+----------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 2 | 18 | zhangsan | 165 | 160 |
| 3 | 17 | wangwu | 170 | 140 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 8 | 22 | wuhan | 190 | 170 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+----------+--------+--------+
9 rows in set (0.00 sec)
(4)查询不在括号内的任何字符
mysql> select * from a where name REGEXP '[^a-z]';
Empty set (0.00 sec)
(5)查询 name 段中至少有一个 i 的数据
mysql> select * from a where name REGEXP 'i{1}';
+------+------+--------+--------+--------+
| id | age | name | height | weight |
+------+------+--------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 4 | 18 | lisi | 180 | 120 |
| 5 | 19 | weipai | 190 | 180 |
| 6 | 20 | zhucii | 150 | 90 |
| 7 | 20 | maliu | 180 | 140 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+--------+--------+--------+
6 rows in set (0.00 sec)
查询 name 段中至少有两个 i 的数据
mysql> select * from a where name REGEXP 'i{2}';
+------+------+--------+--------+--------+
| id | age | name | height | weight |
+------+------+--------+--------+--------+
| 6 | 20 | zhucii | 150 | 90 |
+------+------+--------+--------+--------+
1 row in set (0.00 sec)
(6)查询 name 段某个范围的数据
mysql> select * from a where name REGEXP 'lisi|lisisi';
+------+------+------+--------+--------+
| id | age | name | height | weight |
+------+------+------+--------+--------+
| 4 | 18 | lisi | 180 | 120 |
| 9 | 19 | lisi | 168 | 130 |
+------+------+------+--------+--------+
2 rows in set (0.00 sec)
(7)查询 name 段中至少有一个 m 的
mysql> mysql> select * from a where name REGEXP 'm+';
+------+------+--------+--------+--------+
| id | age | name | height | weight |
+------+------+--------+--------+--------+
| 1 | 16 | liming | 175 | 130 |
| 7 | 20 | maliu | 180 | 140 |
+------+------+--------+--------+--------+
2 rows in set (0.00 sec)
1:用于对记录中的字段值进行运算
2:运算符分类
MySQL支持的算术运算符
示例
(1)加法
mysql> select 6+6;
+-----+
| 6+6 |
+-----+
| 12 |
+-----+
1 row in set (0.00 sec)
(2)多个算数在一起
mysql> select 2+2,8-4,4*4,9/3,3%6;
+-----+-----+-----+--------+------+
| 2+2 | 8-4 | 4*4 | 9/3 | 3%6 |
+-----+-----+-----+--------+------+
| 4 | 4 | 16 | 3.0000 | 3 |
+-----+-----+-----+--------+------+
1 row in set (0.00 sec)
(3)查看算数表结构
ysql> create table suanshu select 2+2,8-4,4*4,9/3,3%6;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc suanshu;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| 2+2 | int(3) | NO | | 0 | |
| 8-4 | int(3) | NO | | 0 | |
| 4*4 | int(3) | NO | | 0 | |
| 9/3 | decimal(5,4) | YES | | NULL | |
| 3%6 | int(1) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
1:字符串的比较默认不区分大小写,可使用binary来区分
2:常用比较运算符
注释
(1)等号 =
示例
mysql> select 1=3,5=5,5='5','a'='b';
+-----+-----+-------+---------+
| 1=3 | 5=5 | 5='5' | 'a'='b' |
+-----+-----+-------+---------+
| 0 | 1 | 1 | 0 |
+-----+-----+-------+---------+
1 row in set (0.00 sec)
结论
(2)不等于运算符
示例
mysql> select 3!=5,3!=3,9<>'9','a'<>'b','a'<>'a';
+------+------+--------+----------+----------+
| 3!=5 | 3!=3 | 9<>'9' | 'a'<>'b' | 'a'<>'a' |
+------+------+--------+----------+----------+
| 1 | 0 | 0 | 1 | 0 |
+------+------+--------+----------+----------+
1 row in set (0.00 sec)
(3) 大于、大于等于、小于、小于等于运算符
mysql> select 6>9;
+-----+
| 6>9 |
+-----+
| 0 |
+-----+
1 row in set (0.00 sec)
mysql> select 6<9;
+-----+
| 6<9 |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql> select 6>=9;
+------+
| 6>=9 |
+------+
| 0 |
+------+
1 row in set (0.00 sec)
mysql> select 6<=9;
+------+
| 6<=9 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select 2>NULL;
+--------+
| 2>NULL |
+--------+
| NULL |
+--------+
1 row in set (0.00 sec)
mysql> select 2=NULL;
+--------+
| 2=NULL |
+--------+
| NULL |
+--------+
1 row in set (0.00 sec)
mysql> select 2>=NULL;
+---------+
| 2>=NULL |
+---------+
| NULL |
+---------+
1 row in set (0.00 sec)
(4) IS NULL、IS NOT NULL
示例
mysql> select 10 is NULL;
+------------+
| 10 is NULL |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)
mysql> select 10 is not NULL;
+----------------+
| 10 is not NULL |
+----------------+
| 1 |
+----------------+
1 row in set (0.00 sec)
结论
(5) BETWEEN AND
BETWEEN AND比较运算通常用于判断一个值是否落在某两个值之间
例如,判断某数字是否在另外两个数字之间,也可以判断某英文字母是否在另外两个字母之间
示例
mysql> select 5 between 3 and 8;
+-------------------+
| 5 between 3 and 8 |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> select 9 between 3 and 8;
+-------------------+
| 9 between 3 and 8 |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> select 'e' between 'a' and 'f';
+-------------------------+
| 'e' between 'a' and 'f' |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select 'o' between 'a' and 'f';
+-------------------------+
| 'o' between 'a' and 'f' |
+-------------------------+
| 0 |
+-------------------------+
1 row in set (0.00 sec)
(6) LEAST、GREATEST
示例
1:比较最小值
mysql> select least(7,8,9);
+--------------+
| least(7,8,9) |
+--------------+
| 7 |
+--------------+
1 row in set (0.00 sec)
mysql> select least('e','f','g');
+--------------------+
| least('e','f','g') |
+--------------------+
| e |
+--------------------+
1 row in set (0.00 sec)
2:比较最大值
mysql> select greatest(7,8,9);
+-----------------+
| greatest(7,8,9) |
+-----------------+
| 9 |
+-----------------+
1 row in set (0.01 sec)
mysql> select greatest('e','f','g');
+-----------------------+
| greatest('e','f','g') |
+-----------------------+
| g |
+-----------------------+
1 row in set (0.00 sec)
结论
(7)IN、NOT IN
示例
mysql> select 3 in (1,3,5);
+--------------+
| 3 in (1,3,5) |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> select 7 in (1,3,5);
+--------------+
| 7 in (1,3,5) |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)
mysql> select 3 not in (1,3,5);
+------------------+
| 3 not in (1,3,5) |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> select 7 not in (1,3,5);
+------------------+
| 7 not in (1,3,5) |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
(8) LIKE、NOT LIKE
示例
mysql> select 'abcd' like 'ab';
+------------------+
| 'abcd' like 'ab' |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> select 'abcd' like '%d';
+------------------+
| 'abcd' like '%d' |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> select 'abcd' like 'ab_d';
+--------------------+
| 'abcd' like 'ab_d' |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)
示例
(1)not 或 !
mysql> select !9;
+----+
| !9 |
+----+
| 0 |
+----+
1 row in set (0.00 sec)
mysql> mysql> select !0;
+----+
| !0 |
+----+
| 1 |
+----+
1 row in set (0.01 sec)
(2)AND 或 &&
mysql> select 5 or 0;
+--------+
| 5 or 0 |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select 5 && 3;
+--------+
| 5 && 3 |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select 5 && 0;
+--------+
| 5 && 0 |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
(3)OR 或 ||
mysql> select 5 or 4;
+--------+
| 5 or 4 |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select 5 or 0;
+--------+
| 5 or 0 |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
(4)XOR
mysql> select 3 xor 0;
+---------+
| 3 xor 0 |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
mysql> select 3 xor 2;
+---------+
| 3 xor 2 |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)
mysql> select 0 xor 0;
+---------+
| 0 xor 0 |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)
结论
(2)逻辑或
(3)逻辑异或
(1)按位与
mysql> select 5 & 3;
+-------+
| 5 & 3 |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
(2)按位或
mysql> select 5 | 3;
+-------+
| 5 | 3 |
+-------+
| 7 |
+-------+
1 row in set (0.00 sec)
(3)按位取反
mysql> select ~5;
+----------------------+
| ~5 |
+----------------------+
| 18446744073709551610 |
+----------------------+
1 row in set (0.00 sec)
(4)按位异或
mysql> select 5 ^ 3;
+-------+
| 5 ^ 3 |
+-------+
| 6 |
+-------+
1 row in set (0.00 sec)
(5)按位左移
mysql> select 5 << 2;
+--------+
| 5 << 2 |
+--------+
| 20 |
+--------+
1 row in set (0.00 sec)
mysql> select 3 << 2;
+--------+
| 3 << 2 |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
(6)按位右移
mysql> select 10 >> 2;
+---------+
| 10 >> 2 |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
1:两张或多张表中同时符合某种条件的数据记录组合
2:FROM子句中使用INNER JOIN关键字连接多张表,并使用ON设置连接条件
3:是系统默认的表连接方式,可以省略INNER关键字
4:多表支持连续使用 INNER JOUN,建议不超过三个表
5:语法结构
SELECT column_name(s)FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
mysql> create table p1(id int(1),height int(3));
Query OK, 0 rows affected (0.00 sec)
mysql> create table p2(id int(1),weight int(3));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into p1 values(1,170),(2,168),(3,160),(4,180),(5,165);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> insert into p2 values(1,170),(2,168),(3,160),(8,140),(9,120),(10,110);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
3:将两张表内连接
mysql> select * from p1 inner join p2 on p1.id=p2.id;
+------+--------+------+--------+
| id | height | id | weight |
+------+--------+------+--------+
| 1 | 170 | 1 | 170 |
| 2 | 168 | 2 | 168 |
| 3 | 160 | 3 | 160 |
+------+--------+------+--------+
3 rows in set (0.00 sec)
1:也被称为左外连接
2:在FROM子句中使用LEFT JOIN关键字来表示
3:匹配左表中所有行及右表中符合条件的行
mysql> select * from p1 left join p2 on p1.id=p2.id;
+------+--------+------+--------+
| id | height | id | weight |
+------+--------+------+--------+
| 1 | 170 | 1 | 170 |
| 2 | 168 | 2 | 168 |
| 3 | 160 | 3 | 160 |
| 4 | 180 | NULL | NULL |
| 5 | 165 | NULL | NULL |
+------+--------+------+--------+
5 rows in set (0.00 sec)
1:也被称为右外连接
2:在FROM子句中使用RIGHT JOIN关键字来表示
3:匹配右表中所有行及左表中符合条件的行
mysql> select * from p1 right join p2 on p1.id=p2.id;
+------+--------+------+--------+
| id | height | id | weight |
+------+--------+------+--------+
| 1 | 170 | 1 | 170 |
| 2 | 168 | 2 | 168 |
| 3 | 160 | 3 | 160 |
| NULL | NULL | 8 | 140 |
| NULL | NULL | 9 | 120 |
| NULL | NULL | 10 | 110 |
+------+--------+------+--------+
6 rows in set (0.00 sec)