【MySQL数据库入门】数据库基本查询操作

1. 学习准备

鉴于本文接下来将介绍数据库查询相关操作,故为了便于读者更好的理解,在此先准备后续演示操作语句所需的数据库、数据表、表数据等。

1.1 创建数据库

-- 创建一个名为test的数据库,并制定为utf-8编码;
-- 注意:charset后应该写utf8而不是utf-8。
create database test charset=utf8;

1.2 使用数据库

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

1.3 显示当前使用的数据库

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)

1.4 创建两个数据表

mysql> -- 记录学生信息的数据表students
mysql> create table students(
	-> id int unsigned primary key auto_increment not null,
	-> name varchar(20) default "",
	-> age tinyint unsigned default 0,
	-> height decimal(5,2),
	-> gender enum("男", "女", "中性", "保密") default "保密",
	-> cls_id int unsigned default 0,
	-> is_delete bit default 0
	-> );
Query OK, 0 rows affected (0.03 sec)
mysql> -- 记录班级信息的数据表classes
mysql> create table classes(
	-> id int unsigned auto_increment primary key not null,
	-> name varchar(30) not null
	-> );
Query OK, 0 rows affected (0.01 sec)

在终端输入创建上述两个数据表的命令后,可通过show tables;语句显示是否创建成功。

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| classes        |
| students       |
+----------------+
2 rows in set (0.00 sec)

1.5 向数据表插入数据

-- 向数据表students中插入14条记录
insert into students values
(0, "小明", 18, 188.88, 2, 1, 0),
(0, "小华", 18, 180.00, 2, 2, 0),
(0, "彭于晏", 29, 185.00, 1, 1, 0),
(0, "刘德华", 59, 175.00, 1, 2, 1),
(0, "黄蓉", 38, 160.00, 2, 1, 0),
(0, "凤姐", 28, 150.00, 4, 1, 0),
(0, "王祖贤", 18, 172.00, 2, 1, 1),
(0, "周杰伦", 36, NULL, 1, 1, 0),
(0, "陈坤", 27, 181.00, 1, 2, 0),
(0, "刘亦菲", 25, 166.00, 2, 2, 0),
(0, "金星", 33, 162.00, 3, 3, 1),
(0, "静香", 12, 180.00, 2, 4, 0),
(0, "郭靖", 12, 170.00, 1, 4, 0),
(0, "周杰", 34, 176.00, 2, 5, 0);
-- 向数据表classes中插入两条记录
insert into classes values(0, "金庸武侠班"), (0, "藤子F不二雄班");

2. 基本查询

2.1 查询数据表中所有记录

mysql> -- 查询数据表students中的所有记录
mysql> select * from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 188.88 ||      1 |           |
|  2 | 小华      |   18 | 180.00 ||      2 |           |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香      |   12 | 180.00 ||      4 |           |
| 13 | 郭靖      |   12 | 170.00 ||      4 |           |
| 14 | 周杰      |   34 | 176.00 ||      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
14 rows in set (0.00 sec)

2.2 查询数据表中指定字段记录

mysql> -- 查询name,age字段对应的数据表记录
mysql> -- select 字段1,... from 数据表名;
mysql> select name, age from students;
+-----------+------+
| name      | age  |
+-----------+------+
| 小明      |   18 |
| 小华      |   18 |
| 彭于晏    |   29 |
| 刘德华    |   59 |
| 黄蓉      |   38 |
| 凤姐      |   28 |
| 王祖贤    |   18 |
| 周杰伦    |   36 |
| 陈坤      |   27 |
| 刘亦菲    |   25 |
| 金星      |   33 |
| 静香      |   12 |
| 郭靖      |   12 |
| 周杰      |   34 |
+-----------+------+
14 rows in set (0.00 sec)
mysql> -- 查询name,age字段对应的数据表记录,并同时分别为字段名name和age起别名
mysql> -- select 字段1 as 别名, 字段2 as 别名 from 数据表名称;
mysql> select name as "姓名", age as "年龄" from students;
+-----------+--------+
| 姓名      | 年龄   |
+-----------+--------+
| 小明      |     18 |
| 小华      |     18 |
| 彭于晏    |     29 |
| 刘德华    |     59 |
| 黄蓉      |     38 |
| 凤姐      |     28 |
| 王祖贤    |     18 |
| 周杰伦    |     36 |
| 陈坤      |     27 |
| 刘亦菲    |     25 |
| 金星      |     33 |
| 静香      |     12 |
| 郭靖      |     12 |
| 周杰      |     34 |
+-----------+--------+
14 rows in set (0.01 sec)
mysql> -- 按字段查询,并去重
mysql> -- select distinct 字段名 from 数据表名;
mysql> select distinct gender from students;
+--------+
| gender |
+--------+
||
||
| 保密   |
| 中性   |
+--------+
4 rows in set (0.00 sec)

3. 条件查询

3.1 比较运算符

SQL语句语法中的比较运算符如下表所示。

比较运算符含义
>大于
<小于
>=大于等于
<=小于等于
=等于
!=或<>不等于
-- 查询数据表students中字段age大于18的记录
select * from students where age>18;
-- 查询数据表students中字段age小于等18的记录
select * from students where age<=18;
-- 查询数据表students中字段age不等于18的记录
select * from students where age!=18;

3.2 逻辑运算符

SQL语句语法中的比较运算符如下表所示。

逻辑运算符含义
and
or
not
-- 查询数据表students中字段age在18到28之间数据记录
select * from students where age>18 and age<28;
-- 查询数据表students中字段age大于18或者字段height大于180(包含)的数据记录
select * from students where age>18 or height>180;
-- 查询数据表students中年龄不是小于或者等于18的女性
select * from students where (not age<=18) and gender=2;

3.3 模糊查询

  • like关键字
    采用like关键字进行模糊查询时,可使用%代替0个、1个或多个字符,使用下划线_代替1个字符。
mysql> -- 查询数据表students中字段name以"小"开始的记录
mysql> select * from students where name like "小%";
+----+--------+------+--------+--------+--------+-----------+
| id | name   | age  | height | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
|  1 | 小明   |   18 | 188.88 ||      1 |           |
|  2 | 小华   |   18 | 180.00 ||      2 |           |
+----+--------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)
mysql> -- 查询数据表students中字段name有两个字符的记录
mysql> select * from students where name like "__";
+----+--------+------+--------+--------+--------+-----------+
| id | name   | age  | height | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
|  1 | 小明   |   18 | 188.88 ||      1 |           |
|  2 | 小华   |   18 | 180.00 ||      2 |           |
|  5 | 黄蓉   |   38 | 160.00 ||      1 |           |
|  6 | 凤姐   |   28 | 150.00 | 保密   |      1 |           |
|  9 | 陈坤   |   27 | 181.00 ||      2 |           |
| 11 | 金星   |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香   |   12 | 180.00 ||      4 |           |
| 13 | 郭靖   |   12 | 170.00 ||      4 |           |
| 14 | 周杰   |   34 | 176.00 ||      5 |           |
+----+--------+------+--------+--------+--------+-----------+
9 rows in set (0.00 sec)
  • rlike正则
mysql> -- 查询数据表students中字段name以"周"开始的记录
mysql> select * from students where name rlike "^周.*";
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |
| 14 | 周杰      |   34 | 176.00 ||      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)

3.4 范围查询

SQL语句语法中支持以下4种方式进行范围查询。

表达式含义
字段名 in (值1, 值2, …)查询字段取值为值1、值2、…的记录
字段名 not in (值1, 值2, …)查询字段取值不为值1、值2、…的记录
字段名 between 值1 and 值2查询字段取值在值1和值2之间的记录
字段名 not between 值1 and 值2查询字段取值不在值1和值2之间的记录
mysql> -- 查询数据表students中字段name取值为12或18或34的记录
mysql> select * from students where age in (12, 18, 34);
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 188.88 ||      1 |           |
|  2 | 小华      |   18 | 180.00 ||      2 |           |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          |
| 12 | 静香      |   12 | 180.00 ||      4 |           |
| 13 | 郭靖      |   12 | 170.00 ||      4 |           |
| 14 | 周杰      |   34 | 176.00 ||      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
6 rows in set (0.00 sec)
mysql> -- 查询数据表students中字段name取值不为12或18或34的记录
mysql> select * from students where age not in (12, 18, 34);
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
+----+-----------+------+--------+--------+--------+-----------+
8 rows in set (0.00 sec)
mysql> -- 查询数据表students中字段name取值在18和34之间的记录
mysql> select * from students where age between 18 and 34;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 188.88 ||      1 |           |
|  2 | 小华      |   18 | 180.00 ||      2 |           |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 14 | 周杰      |   34 | 176.00 ||      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
9 rows in set (0.00 sec)
mysql> -- 查询数据表students中字段name取值不在18和34之间的记录
mysql> select * from students where age not between 18 and 34;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  4 | 刘德华    |   59 | 175.00 ||      2 |          |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |
| 12 | 静香      |   12 | 180.00 ||      4 |           |
| 13 | 郭靖      |   12 | 170.00 ||      4 |           |
+----+-----------+------+--------+--------+--------+-----------+
5 rows in set (0.00 sec)

4. 排序查询

MySQL数据库支持通过order by 字段名实现单、多字段排序查询,且关键字asc表示升序,desc表示降序,分别为英文单词ascend(上升)和descend(下降)的缩写。

4.1. 单字段排序查询

mysql> -- order by 字段名;
mysql> -- 查询students数据表中age字段在18到34之间的男性,字段age的值从大到小排序 
mysql> select * from students where (age between 18 and 34) and gender=1 order by age desc;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |
+----+-----------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)

4.2. 多字段排序查询

-- order by 字段1, 字段2,...
-- 查询students数据表中age字段在18到34之间,gender字段为"女"的记录,
-- 且要求:字段height降序,如果height相同,则继续按照字段age升序。
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;
-- 查询students数据表中age字段在18到34之间,gender字段为"女"的记录,
-- 且要求:字段height降序,如果height相同,则继续按照字段age升序,
-- 如果age也相同,则按照id升序。
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc, id desc;

5. 聚合函数

MySQL支持通过通用统计函数进行查询,常用函数如下表所示。

聚合函数名作用
count计数
max最大值
min最小值
sum求和
avg计算平均值
round指定小数位数,并四舍五入
  • count
mysql> -- 查询students数据表中gender=1的记录有多少条
mysql> select count(*) from students where gender=1;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)
mysql> -- 查询students数据表中gender=1的记录有多少条,并为查询结果起别名
mysql> select count(*) as 男性人数 from students where gender=1;
+--------------+
| 男性人数     |
+--------------+
|            5 |
+--------------+
1 row in set (0.00 sec)
  • max
mysql> -- 查询students数据表中gender字段为2的记录中,age字段的最大值
mysql> select max(age) from students where gender=2;
+----------+
| max(age) |
+----------+
|       38 |
+----------+
1 row in set (0.00 sec)
  • min

类似max

  • sum
-- 计算students数据表中,所有记录中age字段值的和
select sum(age) from students;
  • avg
mysql> -- 计算students数据表中,所有记录中age字段值的平均数
mysql> select avg(age) from students;
+----------+
| avg(age) |
+----------+
|  27.6429 |
+----------+
1 row in set (0.00 sec)
mysql> -- 或者通过求和/总数计算平均值:
mysql> select sum(age)/count(*) from students;
+-------------------+
| sum(age)/count(*) |
+-------------------+
|           27.6429 |
+-------------------+
1 row in set (0.00 sec)
  • round
mysql> -- 计算students数据表中,所有记录中age字段值的平均数,
mysql> -- 并保留三位小数
mysql> select round(sum(age)/count(*),3) from students;
+----------------------------+
| round(sum(age)/count(*),3) |
+----------------------------+
|                     27.643 |
+----------------------------+
1 row in set (0.00 sec)

6. 分组查询

  • group by 字段名
mysql> -- 查询students数据表,按照性别分组,显示所有的性别
mysql> select gender from students group by gender;
+--------+
| gender |
+--------+
||
||
| 中性   |
| 保密   |
+--------+
4 rows in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,显示所有的性别,
mysql> -- 并且统计各个性别的人数
mysql> select gender, count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
||        5 |
||        7 |
| 中性   |        1 |
| 保密   |        1 |
+--------+----------+
4 rows in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,显示所有的性别,
mysql> -- 并且统计各个性别中字段age的平均值
mysql> select gender, avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
||  32.6000 |
||  23.2857 |
| 中性   |  33.0000 |
| 保密   |  28.0000 |
+--------+----------+
4 rows in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,且仅显示男性人数
mysql> select gender, count(*) from students where gender=1 group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
||        5 |
+--------+----------+
1 row in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,仅显示所有男性姓名和年龄
mysql> select gender, group_concat(name, age) from students where gender=1 group by gender;
+--------+-------------------------------------------------------+
| gender | group_concat(name, age)                               |
+--------+-------------------------------------------------------+
|| 彭于晏29,刘德华59,周杰伦36,陈坤27,郭靖12              |
+--------+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,并以更可读方式显示所有男性姓名和年龄
mysql> select gender, group_concat("name:", name, "__", "age:", age) from students where gender=1 group by gender;
+--------+--------------------------------------------------------------------------------------------------------------+
| gender | group_concat("name:", name, "__", "age:", age)                                                               |
+--------+--------------------------------------------------------------------------------------------------------------+
|| name:彭于晏__age:29,name:刘德华__age:59,name:周杰伦__age:36,name:陈坤__age:27,name:郭靖__age:12              |
+--------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • group by 字段名 having 条件
mysql> -- 查询students数据表,按照性别分组,
mysql> -- 且筛选出字段age的平均值大于30的性别组,
mysql> -- 要求显示符合条件分组的所有记录中的name字段值
mysql> select gender, group_concat(name), avg(age) from students group by gender having avg(age)>30;
+--------+---------------------------------------------+----------+
| gender | group_concat(name)                          | avg(age) |
+--------+---------------------------------------------+----------+
|| 彭于晏,刘德华,周杰伦,陈坤,郭靖                  |  32.6000 |
| 中性   | 金星                                         |  33.0000 |
+--------+---------------------------------------------+----------+
2 rows in set (0.00 sec)
mysql> -- 查询students数据表,按照性别分组,
mysql> -- 且筛选出记录数目大于2的性别组,
mysql> -- 且要求显示符合条件分组的所有记录中的name字段值
mysql> select gender, group_concat(name) from students group by gender having count(*)>2;
+--------+--------------------------------------------------------+
| gender | group_concat(name)                                     |
+--------+--------------------------------------------------------+
|| 彭于晏,刘德华,周杰伦,陈坤,郭靖                         |
|| 小明,小华,黄蓉,王祖贤,刘亦菲,静香,周杰                 |
+--------+--------------------------------------------------------+
2 rows in set (0.00 sec)

7. 分页查询

在一般的数据库中,因为其中的数据表中的记录条目可能成千上万条,如:电商平台数据库中的商品条目,为了实现类似分页展示的功能,就需要用到SQL语句中的limit关键字实现分页查询功能。

mysql> -- 语法:limit start[起始记录], count[记录数]
mysql> -- 查询students数据表中前4条记录
mysql> select * from students limit 0, 4;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 188.88 ||      1 |           |
|  2 | 小华      |   18 | 180.00 ||      2 |           |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          |
+----+-----------+------+--------+--------+--------+-----------+
4 rows in set (0.00 sec)
mysql> -- 查询students数据表中第5至8条记录
mysql> select * from students limit 4, 4;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |
+----+-----------+------+--------+--------+--------+-----------+
4 rows in set (0.00 sec)
mysql> -- 查询students数据表中第9至12条记录
mysql> select * from students limit 8, 4;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香      |   12 | 180.00 ||      4 |           |
+----+-----------+------+--------+--------+--------+-----------+
4 rows in set (0.00 sec)

8. 连接查询

8.1. 内连接

内连接可简单理解为:根据两个数据表的字段取值是否相同,进而求取交集数据表。

mysql> -- 查询数据表students和classes,实现:
mysql> -- 将两个数据表通过字段取值存在交集的记录连接在一起
mysql> select * from students inner join classes on students.cls_id=classes.id;
+----+-----------+------+--------+--------+--------+-----------+----+---------------------+
| id | name      | age  | height | gender | cls_id | is_delete | id | name                |
+----+-----------+------+--------+--------+--------+-----------+----+---------------------+
|  1 | 小明      |   18 | 188.88 ||      1 |           |  1 | 金庸武侠班          |
|  2 | 小华      |   18 | 180.00 ||      2 |           |  2 | 藤子F不二雄班       |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           |  1 | 金庸武侠班          |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          |  2 | 藤子F不二雄班       |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           |  1 | 金庸武侠班          |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           |  1 | 金庸武侠班          |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          |  1 | 金庸武侠班          |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           |  1 | 金庸武侠班          |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           |  2 | 藤子F不二雄班       |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           |  2 | 藤子F不二雄班       |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |  3 | 琼瑶班              |
+----+-----------+------+--------+--------+--------+-----------+----+---------------------+
11 rows in set (0.00 sec)

在上述查询中,因为cls_idid信息分属数据表studentsclasses,且二者信息重复显示,现希望仅将数据表classes中的name字段和数据表students连接合并,则:

mysql> -- 查询数据表students和classes,实现:
mysql> -- 将两个数据表通过字段取值存在交集的记录连接在一起
mysql> -- 且仅连接数据表classes的name字段,
mysql> -- 且为数据表classes的name字段重命名为"班级信息"
mysql> select students.*, classes.name as 班级信息 from students inner join classes on students.cls_id=classes.id;
+----+-----------+------+--------+--------+--------+-----------+---------------------+
| id | name      | age  | height | gender | cls_id | is_delete | 班级信息            |
+----+-----------+------+--------+--------+--------+-----------+---------------------+
|  1 | 小明      |   18 | 188.88 ||      1 |           | 金庸武侠班          |
|  2 | 小华      |   18 | 180.00 ||      2 |           | 藤子F不二雄班       |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           | 金庸武侠班          |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          | 藤子F不二雄班       |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           | 金庸武侠班          |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           | 金庸武侠班          |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          | 金庸武侠班          |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           | 金庸武侠班          |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           | 藤子F不二雄班       |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           | 藤子F不二雄班       |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          | 琼瑶班              |
+----+-----------+------+--------+--------+--------+-----------+---------------------+
11 rows in set (0.00 sec)

8.2. 左连接

左连接可理解为:以最左边的待连接数据表为准,根据两个数据表字段的取值是否相同求取数据表,即:得到的数据表中,对二者字段取值相同的记录,则直接连接,对二者字段不同的记录,则以左边数据表的记录为准,右边数据表的字段以null填充。

mysql> -- 查询数据表students中每位学生的班级信息
mysql> select students.*, classes.name as 班级信息 from students left join classes on students.cls_id=classes.id;
+----+-----------+------+--------+--------+--------+-----------+---------------------+
| id | name      | age  | height | gender | cls_id | is_delete | 班级信息            |
+----+-----------+------+--------+--------+--------+-----------+---------------------+
|  1 | 小明      |   18 | 188.88 ||      1 |           | 金庸武侠班          |
|  3 | 彭于晏    |   29 | 185.00 ||      1 |           | 金庸武侠班          |
|  5 | 黄蓉      |   38 | 160.00 ||      1 |           | 金庸武侠班          |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      1 |           | 金庸武侠班          |
|  7 | 王祖贤    |   18 | 172.00 ||      1 |          | 金庸武侠班          |
|  8 | 周杰伦    |   36 |   NULL ||      1 |           | 金庸武侠班          |
|  2 | 小华      |   18 | 180.00 ||      2 |           | 藤子F不二雄班       |
|  4 | 刘德华    |   59 | 175.00 ||      2 |          | 藤子F不二雄班       |
|  9 | 陈坤      |   27 | 181.00 ||      2 |           | 藤子F不二雄班       |
| 10 | 刘亦菲    |   25 | 166.00 ||      2 |           | 藤子F不二雄班       |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          | 琼瑶班              |
| 12 | 静香      |   12 | 180.00 ||      4 |           | NULL                |
| 13 | 郭靖      |   12 | 170.00 ||      4 |           | NULL                |
| 14 | 周杰      |   34 | 176.00 ||      5 |           | NULL                |
+----+-----------+------+--------+--------+--------+-----------+---------------------+
14 rows in set (0.02 sec)

练习:查询students数据表中没有班级信息的记录

mysql> select students.*, classes.name as 班级信息 from students left join classes on students.cls_id=classes.id having classes.name is null;
+----+--------+------+--------+--------+--------+-----------+--------------+
| id | name   | age  | height | gender | cls_id | is_delete | 班级信息     |
+----+--------+------+--------+--------+--------+-----------+--------------+
| 12 | 静香   |   12 | 180.00 ||      4 |           | NULL         |
| 13 | 郭靖   |   12 | 170.00 ||      4 |           | NULL         |
| 14 | 周杰   |   34 | 176.00 ||      5 |           | NULL         |
+----+--------+------+--------+--------+--------+-----------+--------------+
3 rows in set (0.00 sec)

注:关键字wherehaving的区别在于,where用于对某一个数据库中的数据表(如:test数据库中的students数据表)进行条件判断,而having用于对查询得到的结果数据中间表进行条件判断(如studentsclasses左连接后得到的数据表),进而进一步筛选。

8.3. 右连接

在有左连接的情况下,右连接的存在显得没有什么意义,因为只要将两个数据表的位置互换,再使用left join就实现了右连接的目的。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值