MySQL数据库入门(3)——数据库查询操作

文章目录

  • 1. 学习准备
    • 1.1 创建数据库
    • 1.2 使用数据库
    • 1.3 显示当前使用的数据库
    • 1.4 创建两个数据表
    • 1.5 向数据表插入数据
  • 2. 基本查询
    • 2.1 查询数据表中所有记录
    • 2.2 查询数据表中指定字段记录
  • 3. 条件查询
    • 3.1 比较运算符
    • 3.2 逻辑运算符
    • 3.3 模糊查询
    • 3.4 范围查询
  • 4. 排序查询
    • 4.1. 单字段排序查询
    • 4.2. 多字段排序查询
  • 5. 聚合函数
  • 6. 分组查询
  • 7. 分页查询
  • 8. 连接查询
    • 8.1. 内连接
    • 8.2. 左连接
    • 8.3. 右连接

1. 学习准备

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

1.1 创建数据库

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

MySQL数据库入门(3)——数据库查询操作_第1张图片

1.2 使用数据库

use test;

1.3 显示当前使用的数据库

select database();

MySQL数据库入门(3)——数据库查询操作_第2张图片

1.4 创建两个数据表

-- 记录学生信息的数据表students
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
	);
-- 记录班级信息的数据表classes
create table classes(
	id int unsigned auto_increment primary key not null,
	name varchar(30) not null
	);

在终端输入创建上述两个数据表的命令后,可通过show tables语句显示是否创建成功。
MySQL数据库入门(3)——数据库查询操作_第3张图片

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 查询数据表中所有记录

-- 查询数据表students中的所有记录
select * from students;

MySQL数据库入门(3)——数据库查询操作_第4张图片

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

-- 查询name,age字段对应的数据表记录
-- select 字段1,... from 数据表名;
select name, age from students;

MySQL数据库入门(3)——数据库查询操作_第5张图片

-- 查询name,age字段对应的数据表记录,并同时分别为字段名name和age起别名
-- select 字段1 as 别名, 字段2 as 别名 from 数据表名称;
select name as "姓名", age as "年龄" from students;

MySQL数据库入门(3)——数据库查询操作_第6张图片

-- 按字段查询,并去重
-- select distinct 字段名 from 数据表名;
select distinct gender from students;

MySQL数据库入门(3)——数据库查询操作_第7张图片

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个字符。
-- 查询数据表students中字段name以"小"开始的记录
select * from students where name like "小%";
-- 查询数据表students中字段name有两个字符的记录
select * from students where name like "__";

MySQL数据库入门(3)——数据库查询操作_第8张图片

  • rlike正则
-- 查询数据表students中字段name以"周"开始的记录
select * from students where name rlike "^周.*";

MySQL数据库入门(3)——数据库查询操作_第9张图片

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之间的记录
-- 查询数据表students中字段name取值为12或18或34的记录
select * from students where age in (12, 18, 34);
-- 查询数据表students中字段name取值不为12或18或34的记录
select * from students where age not in (12, 18, 34);
-- 查询数据表students中字段name取值在18和34之间的记录
select * from students where age between 18 and 34;
-- 查询数据表students中字段name取值不在18和34之间的记录
select * from students where age not between 18 and 34;

MySQL数据库入门(3)——数据库查询操作_第10张图片
MySQL数据库入门(3)——数据库查询操作_第11张图片

4. 排序查询

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

4.1. 单字段排序查询

-- order by 字段名;
-- 查询students数据表中age字段在18到34之间的男性,字段age的值从大到小排序
select * from students where (age between 18 and 34) and gender=1 order by age desc;

MySQL数据库入门(3)——数据库查询操作_第12张图片

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
-- 查询students数据表中gender=1的记录有多少条
select count(*) from students where gender=1;
-- 查询students数据表中gender=1的记录有多少条,并为查询结果起别名
select count(*) as 男性人数 from students where gender=1;

MySQL数据库入门(3)——数据库查询操作_第13张图片

  • max
-- 查询students数据表中gender字段为2的记录中,age字段的最大值
select max(age) from students where gender=2;
  • min
    类似max

  • sum

-- 计算students数据表中,所有记录中age字段值的和
select sum(age) from students;
  • avg
-- 计算students数据表中,所有记录中age字段值的平均数
select avg(age) from students;
-- 或者通过求和/总数计算平均值:
select sum(age)/count(*) from students;

MySQL数据库入门(3)——数据库查询操作_第14张图片

  • round
-- 计算students数据表中,所有记录中age字段值的平均数,
-- 并保留三位小数
select round(sum(age)/count(*),3) from students;

MySQL数据库入门(3)——数据库查询操作_第15张图片

6. 分组查询

  • group by 字段名
-- 查询students数据表,按照性别分组,显示所有的性别
select gender from students group by gender;
-- 查询students数据表,按照性别分组,显示所有的性别,
-- 并且统计各个性别的人数
select gender, count(*) from students group by gender;
-- 查询students数据表,按照性别分组,显示所有的性别,
-- 并且统计各个性别中字段age的平均值
select gender, avg(age) from students group by gender;

MySQL数据库入门(3)——数据库查询操作_第16张图片

-- 查询students数据表,按照性别分组,且仅显示男性人数
select gender, count(*) from students where gender=1 group by gender;
-- 查询students数据表,按照性别分组,仅显示所有男性姓名和年龄
select gender, group_concat(name, age) from students where gender=1 group by gender;
-- 查询students数据表,按照性别分组,并以更可读方式显示所有男性姓名和年龄
select gender, group_concat("name:", name, "__", "age:", age) from students where gender=1 group by gender;

MySQL数据库入门(3)——数据库查询操作_第17张图片

  • group by 字段名 having 条件
-- 查询students数据表,按照性别分组,
-- 且筛选出字段age的平均值大于30的性别组,
-- 要求显示符合条件分组的所有记录中的name字段值
select gender, group_concat(name), avg(age) from students group by gender having avg(age)>30;

MySQL数据库入门(3)——数据库查询操作_第18张图片

-- 查询students数据表,按照性别分组,
-- 且筛选出记录数目大于2的性别组,
-- 且要求显示符合条件分组的所有记录中的name字段值
select gender, group_concat(name) from students group by gender having count(*)>2;

MySQL数据库入门(3)——数据库查询操作_第19张图片

7. 分页查询

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

-- 语法:limit start[起始记录], count[记录数]
-- 查询students数据表中前4条记录
select * from students limit 0, 4;
-- 查询students数据表中第5至8条记录
select * from students limit 4, 4;
-- 查询students数据表中第9至12条记录
select * from students limit 8, 4;

MySQL数据库入门(3)——数据库查询操作_第20张图片

8. 连接查询

8.1. 内连接

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

-- 查询数据表students和classes,实现:
-- 将两个数据表通过字段取值存在交集的记录连接在一起
select * from students inner join classes on students.cls_id=classes.id;

MySQL数据库入门(3)——数据库查询操作_第21张图片
在上述查询中,因为cls_id和id信息分属数据表students和classes,且二者信息重复显示,现希望仅将数据表classes中的name字段和数据表students连接合并,则:

-- 查询数据表students和classes,实现:
-- 将两个数据表通过字段取值存在交集的记录连接在一起
-- 且仅连接数据表classes的name字段,
-- 且为数据表classes的name字段重命名为"班级信息"
select students.*, classes.name as 班级信息 from students inner join classes on students.cls_id=classes.id;

MySQL数据库入门(3)——数据库查询操作_第22张图片

8.2. 左连接

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

-- 查询数据表students中每位学生的班级信息
select students.*, classes.name as 班级信息 from students left join classes on students.cls_id=classes.id;

MySQL数据库入门(3)——数据库查询操作_第23张图片
练习:查询students数据表中没有班级信息的记录

select students.*, classes.name as 班级信息 from students left join classes on students.cls_id=classes.id having classes.name is null;

MySQL数据库入门(3)——数据库查询操作_第24张图片
注:关键字where和having的区别在于,where用于对某一个数据库中的数据表(如:test数据库中的students数据表)进行条件判断,而having用于对查询得到的结果数据表进行条件判断(如students和classes左连接后得到的数据表),进而进一步筛选。

8.3. 右连接

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

你可能感兴趣的:(MySQL)