MySQL高级SQL语句 第一篇

文章目录

  • MySQL 高级 SQL 语句操作
    • 一:按关键字排序
    • 二:对结果进行分组
    • 三:限制结果条目
    • 四:设置别名
    • 五:通配符
    • 六:子查询
    • 七:NULL值
    • 八:正则表达式
    • 九:运算符
      • 1:算术运算符
      • 2:比较运算符
      • 3:逻辑运算符
      • 4:位运算符
      • 5:运算符的优先级
    • 十:表的连接方式
      • 1:内连接
      • 2:外连接
        • (1):左连接
        • (2):右连接

MySQL 高级 SQL 语句操作

一:按关键字排序

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 进行字段查询

  • 对 name 和 age 查询并对 age 降序查看
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)
  • 对 name 和 age 查询并对 age 升序查看
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 进行多字段查询

  • 对age,name,height 对字段降序查看
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)
  • 对age,name,height 对字段升序查看
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分组示例

  • 对 weight 大于 140 的 name 和 age 进行分组
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

  • 对 weight 大于 120 的 name 和 age 进行分组,并对 name 分组进行降序排序
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)限制结果的行数

  • 限制表的前 5 行
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)不从第一条开始取值

  • 不从 1 开始,取值后面的 5 行
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 的第二种操作

  • 设置别名,id 设为 aaa,height 设为 bbb,weight 设为 ccc ,表 a 设为 表 aa
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)
  • 设置别名,id 设为 aaa,height 设为 bbb,weight 设为 ccc ,表 a 设为 表 aa,只显示 height
    大于等于 180 的
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)
  • 设置别名,查询前3行
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)使用 % 进行检索查询

  • 查询表里以 l 开头的 name
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)
  • 查询表里以 n 结尾的 name
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)通配符_的用法

  • 查询 name 里的单个字符
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)查询列表信息

  • 查询 name 和 height 两列并列出 height 大于 180 的
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)
  • 查询 name 和 height 两列并列,后面不添加条件
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

  • 将表 a 中的 weight 大于等于 140 的插入给表 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)定义别名根据条件查询

  • 定义别名为 b ,查询 height 大于等于 170 的以 name 和 height 列表显示
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)
  • 使用 update 对 a1 表里符合条件的 height 的数值加 10 (条件为 height 值大于等于180)
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)
  • 删除 height 值大于等于 180 小于等于 200 的值将被删除
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)

七:NULL值

1:表示缺失的值

2:与数字0或者空白(spaces)是不同的

3:使用IS NULL或IS NOT NULL进行判断

4:NULL值和空值的区别

  • 空值长度为0,不占空间;NULL值的长度为NULL,占用空间
  • IS NULL无法判断空值
  • 空值使用“=”或者“<>”来处理
  • COUNT()计算时,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 值为空

  • 添加 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)
  • 验证:添加后一共有 6 行,查询 weight 行时为5,是因为 NULL 不计数
mysql> select count(weight) from a1;
+---------------+
| count(weight) |
+---------------+
|             5 |
+---------------+
1 row in set (0.01 sec)

(2)测试 0 占位

  • 添加带 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)
  • 验证:height 值有 0 时,也占用空间
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:常用匹配模式

  • ^匹配开始字符
  • p1|p2匹配p1或p2
  • $匹配结束字符
  • […]匹配字符集中的任意一个字符
  • .匹配任意单个字符
  • [^…]匹配不在中括号内的任何字符
  • *匹配任意个前面的字符
  • {n}匹配前面的字符串n次
  • +匹配前面字符至少1次
  • {n,m} 匹配前面的字符串至少n次,至多m次

以此表为例进行正则表达式操作

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:运算符分类

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符

1:算术运算符

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)

2:比较运算符

1:字符串的比较默认不区分大小写,可使用binary来区分

2:常用比较运算符

  • = 等于
  • < 小于、>大于
  • <= 小于等于、>=大于等于
  • !=或 <> 不等于
  • IN 在集合中
  • LlKE 通配符匹配
  • IS NULL 判断一个值是否为NULL
  • IS NOT NULL判断一个值是否不为NULL
  • BETWEEN AND 两者之间
  • GREATEST两个或多个参数时返回最大值
  • LEAST 两个或多个参数时返回最小值

注释

(1)等号 =

  • 是用来判断数字、字符串和表达式是否相等的,如果相等则返回1,如果不相等则返回0
  • 如果比较的两者有一个值是NULL,则比较的结果就是NULL
  • 其中字符的比较是根据ASCII码来判断的,如果 ASCII码相等,则表示两个字符相同;如果ASCII码不相等,则表示两个字符不相同

示例

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)

结论

  • 如果两者都是整数,则按照整数值进行比较
  • 如果一个整数一个字符串,则会自动将字符串转换为数字,再进行比较
  • 如果两者都是字符串,则按照字符串进行比较
  • 如果两者中至少有一个值是 NULL,则比较的结果是 NULL

(2)不等于运算符

  • 不等于号有两种写法,分别是<>或者=,用于针对数字、字符串和表达式不相等的比较
  • 如果不相等则返回1,如果相等则返回0,这点正好跟等于的返回值相反
  • 需要注意的是不等于运算符不能用于判断 NULL

示例

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) 大于、大于等于、小于、小于等于运算符

  • 大于(>)运算符用来判断左侧的操作数是否大于右侧的操作数,若大于返回1,否则返回0,同样不能用于判断NULL
  • 小于(<) 运算符用来判断左侧的操作数是否小于右侧的操作数,若小于返回1,否则返回0,同样不能用于判断NULL
  • 大于等于(>=)判断左侧的操作数是否大于等于右侧的操作数,若大于等于返回1,否则返回0,不能用于判断NULL
  • 小于等于(<=) 判断左侧的操作数是否小于等于右侧的操作数,若小于等于返回1,否则返回0,不能用于判断NULL
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

  • IS NULL判断一个值是否为 NULL,如果为 NULL返回1,否则返回0
  • IS NOT NULL判断一个值是否不为NULL,如果不为 NULL返回1,否则返回0

示例

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)

结论

  • IS NULL和IS NOT NULL 一个判断为空,另一个判断不为空,只是有无 NOT 这个关键字的区别,同时返回值不同。

(5) BETWEEN AND

  • BETWEEN AND比较运算通常用于判断一个值是否落在某两个值之间

  • 例如,判断某数字是否在另外两个数字之间,也可以判断某英文字母是否在另外两个字母之间

示例

  • 5 在3 与 8 之间
mysql> select 5 between 3 and 8;
+-------------------+
| 5 between 3 and 8 |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)
  • 9 不在 3 与 8 之间
mysql> select 9 between 3 and 8;
+-------------------+
| 9 between 3 and 8 |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)
  • e 在 a 与 f 之间
mysql> select 'e' between 'a' and 'f';
+-------------------------+
| 'e' between 'a' and 'f' |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)
  • o 不在 a 与 f 之间
mysql> select 'o' between 'a' and 'f';
+-------------------------+
| 'o' between 'a' and 'f' |
+-------------------------+
|                       0 |
+-------------------------+
1 row in set (0.00 sec)

(6) LEAST、GREATEST

  • LEAST:当有两个或者多个参数时,返回其中的最小值。如果其中一个值为NULL,则返回结果就为NULL
  • GREATEST:当有两个或者多个参数时,返回其中的最大值。如果其中一个值为NULL,则返回结果就为NULL

示例

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)

结论

  • LEAST 比较的参数为数字时,返回的是其中最小的值;当比较的参数为字符串时,返回字母表中顺序最靠前的字符
  • GREATEST比较的参数为数字时,返回的是其中最大的值;当比较的参数为字符串时,返回字母表中顺序最靠后的字符

(7)IN、NOT IN

  • IN判断一个值是否在对应的列表中,如果是返回1,否则返回0
  • NOTIN判断一个值是否不在对应的列表中,如果不是返回1,否则返回0

示例

  • 判断 3 是否在数值里面,为真则返回 1
mysql> select 3 in (1,3,5);
+--------------+
| 3 in (1,3,5) |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)
  • 判断 7 是否在数值里面,为假则返回 0
mysql> select 7 in (1,3,5);
+--------------+
| 7 in (1,3,5) |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)
  • 判断 3 不在数值里面,为假则返回 0
mysql> select 3 not in (1,3,5);
+------------------+
| 3 not in (1,3,5) |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)
  • 判断 7 不在数值里面,为真则返回 1
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

  • LIKE 用来匹配字符串,如果匹配成功则返回1,反之返回O
  • LIKE 支持两种通配符:‘%’ 用于匹配任意数目的字符;而 ‘_’ 只能匹配一个字符
  • NOT LIKE正好跟LlKE 相反,如果没有匹配成功则返回1,反之返回0

示例

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)

3:逻辑运算符

  • 逻辑运算符又被称为布尔运算符,通常用来判断表达式的真假
  • 如果为真返回1,否则返回0,真和假也可以用 TRUE 和 FALSE 表示
  • MySQL 中支持使用的逻辑运算符有四种
    MySQL高级SQL语句 第一篇_第1张图片
    1:逻辑非
  • 逻辑运算符中最简单的运算符就是逻辑非,逻辑非使用NOT或!表示
  • 逻辑非将跟在它后面的逻辑测试取反,把真变为假,把假变为真
  • 如果 NOT后面的操作数为0时,所得值为1;如果操作数为非0时,所得值为0;如果操作数为NULL时,所得值为NULL

示例

(1)not 或 !

  • 9 为真非 0 值;为 1,1 取反就是 0
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 或 &&

  • 5 为非 0 值,统一为真,真的话就是 1
mysql> select 5 or 0;
+--------+
| 5 or 0 |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)
  • 5 和 3 都为非 0 值,两个真值相与便为真
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 或 ||

  • 两个数值类型都为非 0 值,为真
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)
  • 两个非 0 值也为假
mysql> select 0 xor 0;
+---------+
| 0 xor 0 |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

结论

  • 异或:同为真或同为假都为假,一个真一个假时才为真。

(2)逻辑或

  • 逻辑或表示包含的操作数,任意一个为非零值并且不是NULL值时,返回1,否则返回0
  • 逻辑或通常使用 OR 或者 || 来表示

(3)逻辑异或

  • 两个非 NULL值的操作数,如果两者都是О或者都是非0,则返回0
  • 如果一个为0,另一个为非0,则返回结果为1
  • 当任意一个值为NULL 时,返回值为 NULL

4:位运算符

  • 位运算符实际上是对二进制数进行计算的运算符
  • MySQL 内位运算会先将操作数变成二进制格式,然后进行位运算,最后在将计算结果从二进制变回到十进制格式,方便用户查看
  • MySQL支持6种位运算符
    MySQL高级SQL语句 第一篇_第2张图片

(1)按位与

  • 5 的二进制为 101;3 的二进制位 011,相乘得出 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)按位异或

  • 5 为101,3 为 011,1 与 0 异或为 1;0 与 1 异或为 1;1 与 1 异或为 0,结果为 110,换算成 6
mysql> select 5 ^ 3;
+-------+
| 5 ^ 3 |
+-------+
|     6 |
+-------+
1 row in set (0.00 sec)

(5)按位左移

  • 5 为 101,左移两位为 10100,换算成 20
mysql> select 5 << 2;
+--------+
| 5 << 2 |
+--------+
|     20 |
+--------+
1 row in set (0.00 sec)
  • 3 为 011,左移两位为 01100,换算成 12
mysql> select 3 << 2;
+--------+
| 3 << 2 |
+--------+
|     12 |
+--------+
1 row in set (0.00 sec)

(6)按位右移

  • 10 为 1010,右移两位为 10,换算成 2
mysql> select 10 >> 2;
+---------+
| 10 >> 2 |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

5:运算符的优先级

  • 决定了不同的运算诱在计算过程中的先后顺序
  • 优先级高的先进算。同级的按从左到右进行计算
  • 可以使用()小括号来改变计算优先级

MySQL高级SQL语句 第一篇_第3张图片

十:表的连接方式

1:内连接

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高级SQL语句 第一篇_第4张图片
示例

  • 1:新建两张表
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)
  • 2:往两张表里添加数据
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)

2:外连接

(1):左连接

1:也被称为左外连接

2:在FROM子句中使用LEFT JOIN关键字来表示

3:匹配左表中所有行及右表中符合条件的行

4:实现原理
MySQL高级SQL语句 第一篇_第5张图片
示例

  • 将内连接创建的两张表左连接
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)

(2):右连接

1:也被称为右外连接

2:在FROM子句中使用RIGHT JOIN关键字来表示

3:匹配右表中所有行及左表中符合条件的行

4:实现原理
MySQL高级SQL语句 第一篇_第6张图片
示例

  • 将内连接创建的两张表右连接
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)

你可能感兴趣的:(mysql)