软件测试-MySQL语言基础-1表的创建create、查select、增insert、删delete、改update

数据库介绍

数据库是指长期存储在计算机内、有组织的数据集合。简而言之,数据库就是一个存储数据的地方。

数据库中存储数据的基本单位是数据表,数据按照分类存储到不同的表中,能够非常搞笑的查询其中的数据。

关系型数据库

所谓的关系型数据库,是由多张有关联的表组成的数据库。

20250428183335690-image

关系型数据库的主要产品

  • oracle:在大型项目中使用,银行、电信等项目
  • MySQL:web时代使用最广泛的关系型数据库。
  • Microsoft sql server:在微软平台的项目中常用。
  • sqlite:轻量级数据库,主要应用在移动平台。

关系型数据库的核心元素

  • 数据库(表的集合,一个数据库中能够有多个表)
  • 表(由行和列组成的二维表格)
  • 行(记录)
  • 列(字段)

20250428183354511-image

sql语言

定义

是一款结构化查询语言。

sql是一门特殊的语言,专门用来操作关系型数据库,当前关系型数据库都支持使用sql语言进行操作,也就是说可以通过sql语言操作Oracle、MySQL、SQL server、sqlite等所有的关系型数据库。

sql语言主要分为:

  • DQL:数据查询语言,用于对数据进行查询,如select;
  • DDL:数据定义语言,进行数据库、表的管理等,如create、drop;
  • DML:数据操作语言,对数据进行增删改,如insert、update、delete;
  • TPL:事务处理语言,对事物进行处理,包括begin transaction、commit、rollback;

对于测试工程师来讲,重点是数据库的查询,需要熟练编写DQL数据查询语言,能看懂DDL数据定义语言,DML和其它语句连接即可。

注意:

sql语言不区分大小写。

navicat工具的使用

navicat介绍:为图形化界面客户端的使用,需要熟练掌握。MySQL语言基础

1.连接本机的MySQL

  1. 打开navicat程序,点击连接-MySQL

    20250428184458563-image

  2. 在弹出的窗口,输入连接名(随便起)、密码(安装MySQL时设置的密码),点击链接测试

    20250428184633431-image

  3. 测试成功后,点击确定按钮

    20250428184657277-image

  4. 双击之前的连接名(local),就可以看到本机mysql中所有的仓库

    20250428184742313-image

2.数据库操作

  1. 创建数据库,打开navicat,双击连接名(local),此时已经连接上服务端,鼠标右键点击连接名,点击新建数据库。

    20250428184922769-image

  2. 输入数据库名,字符集选择utf-8,排序规则选择utf8_general_ci

    20250428185033300-image

  3. 数据库操作,鼠标右键点击某个数据库(student),点击打开数据库,后面对数据库的操作都需要在数据库打开的状态下进行。

    20250428185214824-image

  4. 修改数据库,鼠标右键点击某个数据库,点击编辑数据库

    20250428185249287-image

  5. 可以修改字符集和排序规则,数据库名不能修改

    20250428185318206-image

  6. 删除数据库,鼠标右键点击某个数据库下面的查询按钮,然后点击新建查询。

    20250428185359460-image

  7. 在打开的查询编辑器中,编写sql语言,再点击运行。

    20250428185435697-image

  8. 运行第一个sql语句,select now();显示系统当前的时间和日期。
    1. 在navicat中可以按住ctrl+r,通过快捷键运行编写的sql代码。

      20250428185614544-image

  9. 到此为止,navicat基本介绍完毕。

SQL语言基础

掌握create table、drop table语句;

掌握:增加insert、查询select、删除delete、修改uodate表语句。

1. sql语言中的注释

  • — 行注释
  • /* 注释内容 */

注意:

  • 在navicat中按住ctrl+/快速注释选中的sql代码
  • 在navicat中按住ctrl+shiftl+/ 选中sql代码取消注释。

2. MySQL的常用数据类型

  • 整数:int,
    有符号范围(-2147483648,2147483647)可以表示 ​正数、负数和零。
    无符号范围(0,4294967295)只能表示 ​正数和零。
    比如 int unsigned,代表设置一个无符号的整数。
    【如果字段类型后面没有 UNSIGNED,默认是 ​有符号,如果字段类型后面加上 UNSIGNED,就是 ​无符号
    CREATE TABLE example (
        id INT,                  -- 默认是有符号(signed)
        age TINYINT UNSIGNED,    -- 无符号(unsigned)
        balance INT SIGNED       -- 显式指定有符号(signed)
    );
  • 小整数:tinyint
    有符号范围(-128,127),
    无符号范围(0,255),
    如tinyint unsigned,代表设置一个无符号。
    【如果字段类型后面没有 UNSIGNED,默认是 ​有符号,如果字段类型后面加上 UNSIGNED,就是 ​无符号
  • 小数:deimal
    如decimal(5,2)表示共存5位数,小数占2位,不能超过2位,整数占3位,不能超过3位。
    CREATE TABLE example (
        price DECIMAL(5, 2)  -- 价格字段,最多 5 位数,其中小数部分最多 2 位
    );
    
    INSERT INTO example (price) VALUES (123.45);  -- 合法
    INSERT INTO example (price) VALUES (99.99);   -- 合法
    INSERT INTO example (price) VALUES (1234.56); -- 非法,会报错
  • 字符串:varchar
    如varchar(3),表示最多存3个字符,一个中文或一个字母都占一个字符。
  • 日期时间:datetime
    范围(1000-01-01 00:00:00~999-12-31 23:29:29)如:2020-01-01 12:29:59

数据库中的基本元素及使用

  • 数据库–database
  • 表–table
    数据库中存储数据的基本单元,表是一个由行和列组成的二维表格结构。
  • 字段(列)–field
    表中的列,在数据库中,叫做字段。
  • 记录(行)–record
    表中的一行,在数据库中,叫做记录。

1. create table 创建表

  • 语法
    create table 表名 (字段名 数据类型,字段名 数据类型)
-- 例 1: 创建表a ,字段要求:name(姓名),数据类型:varchar(字符串),长度为10
CAEATE TABLE a (name varchar(10))

执行这个语句后,数据库中将创建一个名为 a 的表,该表只有一个列 name,该列可以存储最多 10 个字符的字符串。

  • 创建两个字段(列)的表
-- 例 2: 创建表b,字段要求:name(姓名),数据类型为varchar(字符串),长度为10.
-- height(身高),数据类型为decimal(小数),一共5位数,其中三位数是整数,2位是小数。
CREATE TABLE b(
    name varchar(10),
    height DECIMAL(5,2)
);

在数据库中创建一个名为 b 的表,表中有两列:

  1. name 列:用于存储字符串类型的名字,最大长度为 10 个字符。
  2. height 列:用于存储数值类型的身高,总位数为 5 位,其中小数部分占 2 位。
  • 创建三个字段(列)的表
--例 3:创建表C,字段要求如下:
-- id:数据类型为 int(整数)
-- name 姓名:数据类型为 varchar(字符串)长度为 20,
-- age 年龄:数据类型为 tinyint unsigned(无符号小整数);
CREATE TABLE c(
    id int,
    name VARCHAR(20),
    age TINYINT UNSIGNED
);

这条 SQL 语句的意思是:​创建一个名为 c 的表,表中有三列:​

  1. id 列:用来存整数(比如 1, 2, 3 等)。
  2. name 列:用来存名字,名字最多可以写 20 个字符(比如 “张三”、”Alice”)。
  3. age 列:用来存年龄,年龄必须是一个 0 到 255 之间的整数(比如 25, 30 等)。

2. insert 插入\添加数据

  • 语法:insert into 表名 values(值,值,值);
-- 向表c 中插入一条记录
INSERT into c VALUES(0,'张飞',30);

这条 SQL 语句的意思是:​向表 c 中插入一行数据,id 列为 0name 列为 '张飞'age 列为 30。​ 需要注意的是,逗号必须使用英文逗号,否则会报错。

  • 使用指定字段插入,语法insert into 表名(字段名,字段名)values(值,值);
-- 向表c 插入一条记录,只设置id 和姓名 name
INSERT into c (id,name) values (3,'曹操');

这条 SQL 语句的意思是:​向表 c 中插入一行数据,id 列为 3name 列为 '曹操'age 列未指定值(默认为 NULL)。

-- 向表c 插入一条记录,只设置 id 和 age
INSERT into c (id,age) values (4,100);

这条 SQL 语句的意思是:​向表 c 中插入一行数据,id 列为 4age 列为 100name 列未指定值(默认为 NULL)。

2.1 插入多条记录

  • 写多条 insert 语句使用英文;分开。
    insert into c values();
    ……
  • 比如:像表c中插入三条 记录,写三条insert语句,语句之间用英文分号分开。
    insert into c values(1,’你好’,10);
    INSERT INTO c VALUES(2,’哈喽’,20);
    INSERT INTO c VALUES(3,’世界’,30)

2.2 一条insert 语句插入多条记录。

  • 语法
    insert into 表名 values(…),(…),(…)

比如:向表c中插入多条记录,用一条insert语句,数据之间用逗号分割。

  • insert into c values(4,’张三’,11),(5,’李四’,12),(5,’王五’,13);

2.3 写一条insert语句,设置指定字段值

  • 语法
    insert into 表名(字段1…) values(值1,…),(值2,…)…;

比如:表c插入多条记录,用一条insert语句,只设置姓名和年龄。

insert into c(name,age) values(‘刘备’,26),(‘张飞’,35);

3.select 简单查询

查询所有字段:

  • 语法
  • select * form 表名

例如:查询表c中的所有数据。

  • select * from c;

查询指定字段:

  • 语法
  • select 字段1,字段2… from 表名;

例如:查询表c中的姓名字段(列)和年龄字段(列)

  • select name,age from c;

4.update 修改数据

  • 语法
  • update 表名 set 字段=值,字段=值 where 条件;
  • 如果没有where条件,代表修改表中所有的记录。

比如:修改表c,所有人的年龄age字段为50。

  • update c set age=50;

比如:修改表c,id为2的记录,姓名name字段改为狄仁杰,年龄age字段改为20.

  • update c set name=’狄仁杰’,age=20 where id=2;

比如:将表c中name为刘备的姓名修改为李白。

  • update c set name=’李白’ where name=’刘备’;

比如:id大于10的记录,增加一岁。

  • update c set age = age + 1 where id > 10

5.delete 删除表中的记录

  • 语法
  • delete from 表名 where 条件;

比如:删除表c中id为6的记录。

  • delete from c where id=6;

比如:删除表c中age大于10的值。

  • delete from c where age > 10;

比如:删除表c中所有的记录。

  • delete from c;

6.truncate 删除表中的记录

  • 语法
  • truncate table 表名;

比如:删除表c的所有数据。

  • truncate table c;

注意事项:

delete和truncate的区别。

  • 在速度上,truncate > delete;
  • 如果想删除部分数据用delete,注意带上where子句;
  • 如果想要保留表而将所有数据删除,自增长字段恢复从1开始,用truncate;

7.drop table删除表

语法1:

  • drop table 表名;

比如:删除表a。

  • drop table a;

语法2:

  • drop table if exists 表名;
  • 如果表存在,就删除表。如果不存在,则不做任何操作。

比如:删除表b。

  • drop table if exists b;

字段的约束

1.常用约束介绍

  • 主键(primary key):值不能重复,auto_increment代表值自动增长。
  • 非空(not null):此字段不允许填写空值。
  • 唯一(unique):此字段的值不允许重复。
  • 默认值(default):当不填写此值时,会使用默认值,如果填写时以填写为准。

2.创建带约束字段的语法格式

crate table 表名(字段名 数据类型 约束,字段名 数据类型 约束…);

1. 主键与自增长

  • 带有primary key(主键)的字段,值不能重复。
  • auto_increment为自增长。
  • 语法
    create table 表名(字段名 数据类型 主键(primary key) 自动增长auto_increment,字段名 数据类型 约束);

比如:创建表a,并向表a中插入数据,字段要求如下。

  • id:数据类型为int unsigned(无符号整数),primary key(主键)auto_increment(自增长);
  • name:数据类型为varchar(字符串)长度为10;
  • age:数据类型为int(整数);
  • 语法
    • create table a (
      id int unsigned primary key auto_increment,
      name varchar(10),
      age int
      ); 

主键并且自增长字段的表,insert语句插入数据,如果不指定id字段,id字段的值会自增长。

例如:不指定id字段值,系统自动填写。

  • insert into a(name,age) values(‘大苏打’,80);

如果插入数据时,插入所有字段,但又没写自增长字段的值,insert语句会报错。

比如:所有字段都插入,但没写自增长字段值,该语句会出现错误。

  • insert into a values(‘哈喽’,66);

解决方案

使用占位符,通常使用0或者unll(空)来占位。

比如:使用0或者null来占位,实现自增长字段的插入。

  • insert into a values(null,’哈喽’,66);

2. 非空 — not null

带有not null(非空)的字段,表示值不能为空,必须要有值。

语法

creat table 表名(字段名 数据类型 not null,….);

比如:创建表e,字段要求如下。

id:数据类型为int unsigned(无符号整数);

name:数据类型为varchar(字符串),长度为10,not null(非空),

age:数据类型为int(整数)

使用语法

create table e(
    id int unsigned,
    name varchar(10) not null,
    age int
);

非空字段表,insert 插入书一定要指定字段值,不然会插入失败。

比如:insert 插入数据,没有指定name 的值会失败,因为name字段not null(非空)

  • insert into e (id,age) values(2,25);

比如:表e正确的插入数据语句。

  • insert into e values(1,’你好’,33);

3. 唯一–uniqe

唯一(unique),表示此字段的值不允许重复。

语法

create table 表名(
    字段名  数据类型  unique,
    ….
);

比如:创建表f,字段要求如下。

id:数据类型为int(整数),

name:数据类型为varchar(字符串)长度为10,unique(唯一),

age:数据类型为int(整数)

create table f(
    id int,
    name varchar(10) unique,
    age int
);

唯一字段的表,insert语句插入数据。

比如:表f中插入一条记录

  • insert into f values(1,’嘉庆’,33);

比如:如果插入的记录中name的值重复,会插入失败。

insert into f values(2,’嘉庆’,50);

4. 默认值

默认值(default),表示当不填写此值时会使用默认值,如果填写时已填写为准。

语法

create table 表名(
    字段名 数据类型 default 值,
    …..
);

比如:创建表g,字段要求如下。

id:数据类型为int(整数);

name:数据类型为varchar(字符串)长度为10,

age:数据类型为int(整数),default(默认值)30。

create table g(
    id int,
    name varchar(10),
    age int default 30
);

默认值字段的表,insert插入数据

比如:age字段的默认值为30.

如果insert 没有指定age 的值,那么age 的值则为默认值 30.

insert into g(id,name) values(1,’王五’);

比如:insert指定了age 的值为50,最终写入表中的数据为50.

insert into g values(2,’你好’,50);

别名与重复记录–as–distint

数据准备

-- 如果学生表students存在,就删除学生表students
DROP table if EXISTS students;


-- 创建students学生表
create table students (
  -- 学号,数据类型(字符串)长度为10,且值不能重复
  students varchar(10) PRIMARY KEY, 
  -- 姓名 数据类型(字符串)长度为10
  NAME varchar(10), 
  -- 性别 数据类型(字符串)长度为1
  sex varchar(1), 
  -- 家乡 数据类型(字符串)长度为20
  hometown varchar(20), 
  -- 年龄 数据类型(小整数)
  age tinyint, 
  -- 班级 数据类型(字符串)长度为10
  class varchar(10), 
  -- 身份证号码 数据类型 (字符串)长度为20
  card varchar(20) 
);

-- 向students表中插入数据
insert into students values
('001', '王昭君', '女', '北京', '30', '1班', '110101199003157654'),
('002', '诸葛亮', '男', '上海', '29', '2班', '310102199104262354'),
('003', '张飞', '男', '南京', '30', '3班', '320102199003047654'),
('004', '白起', '男', '安徽', '35', '4班', '340202198505177654'),
('005', '大乔', '女', '天津', '28', '3班', '120101199204067654'),
('006', '孙尚香', '女', '河北', '25', '1班', '130502199506137654'),
('007', '百里玄策', '男', '山西', '39', '2班', '140102198107277654'),
('008', '小乔', '女', '河南', '25', '3班', null),
('009', '百里守约', '男', '湖南', '31', '1班', ''),
('010', '妲己', '女', '广东', '24', '2班', '440701199607147654'),
('011', '李白', '男', '北京', '30', '4班', '110202199005017754'),
('012', '孙膑', '男', '新疆', '36', '3班', '650102198401297655');

select * from students

20250430133502720-image

1. 查询所有字段

比如:擦汗寻students 表中的所有字段

  • select * from students;

2. 查询指定字段

比如:查询students表中的name,sex和age字段

  • select name,sex,age from students;

3. 字段的别名-as

1. 在select后面的字段部分,可以使用as为字段起别名,这个字段的别名出现在select查询结果中。

比如:students表的name字段别名为 姓名,sex字段别名为 性别,age字段的别名为 年龄。

  • select name as 姓名,sex as 性别, age as 年龄 from students;

    20250430133410253-image

2. as 可以省略。

比如:students表的name字段别名为 姓名,sex字段别名为 性别,age字段的别名为 年龄。

  • select name 姓名, sex 性别, age 年龄 from students;

4. 表的别名- as

1.在from后面的表名,可以使用 as 为表起别名。

比如:students表的别名为 s

  • select * from students as s;

    20250430133336766-image

2. as 可以省略。

比如:students 表的别名为 s

  • select * from students s;

5. 消除重复记录 – distinct

1.在select后面的字段前使用 distinct 可以消除重复的记录。

语法格式:
  • select distinct 字段 from 表名;

比如:查询students表的 sex 字段,用distinct 取消重复记录。

  • select distinct sex from students

    20250430133259386-image

条件查询

1.where子句的使用

使用where子句可以对表中的数据筛选,符合条件额数据会出现在结果集中。

语法格式:

select 字段,字段 from 表名 where 条件;

比如:查询students表中学号(students)等于 001 的记录。

  • select students from students where students=’001′;

    20250430134037374-image

比如:查询students 表中年龄 age 等于 30 的姓名和班级。

  • select name,class,age from students where age = 30;

    20250430134342847-image

where子句使用注意事项:

  • select 后面的 * 或者字段名,决定了返回什么样的字段【列】;
  • select 中的where 子句,决定了返回什么样的记录【行】。

where 后面支持多种运算符,进行条件的处理。

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

1. 比较运算符–>–>=–<=–<–=–!=–<>

  • 等于:=
  • 大于:>
  • 大于等于:>=
  • 小于:<
  • 小于等于:<=
  • 不等于:!= 或者 <>

比如:查询students 表中name 姓名等于小乔学生的age年龄。

  • select age from students where name= ‘小乔’;

    20250430135049442-image

比如:查询students 表中 30 岁以下的学生记录

  • select * from students where age < 30;

    20250430135315605-image

比如:查询students表中class班级为 1 班以外的学生记录。

  • select * from students where class != ‘1班’;

    20250430135638563-image

2. 逻辑运算符–and–or–not

  • and(与-并且)
    • and有两个条件
    • 条件1 or 条件2
    • 两个条件只要有一个满足即可

比如:查询age 年龄小于 30,并且 sex 性别 为 女 的同学记录。

代码:select * from students where age < 30 and sex = '女';

20250430140000970-image

  • or(或)
    • or(或) 有两个条件
    • 条件1 or(或) 条件2
    • 两个条件只要有一个满足即可。

比如:查询sex 性别为 女 或者 class 班级 为 1班 的学生记录。

代码:select * from students where sex='女' or class = '1班';

20250430140440182-image

  • not(非)
    • not 只有一个条件
    • not 条件
    • 如果条件为满足,not 后变为不满足。如果条件 为不满足,not 后变为满足。

比如:查询 hometown 老家 非 ’天津‘ 的学生记录。

代码写法1:select * from students where not hometown='天津';

代码写法1:select * from students where hometown != '天津';

20250430141128982-image

3. 模糊查询 — like

  • 关键字 like 实现模糊查询
  • % 表示任意多个任意字符
  • _ 表示一个任意字符。
  • 格式 字段名 like  字符_ %

比如:查询name姓名中 以 孙 开头的学生记录

代码:select * from students where name like '孙%';

20250430145150431-image

比如:查询name 姓名 以 孙 开头,并且只有一个字的学生记录

代码:select * from students where name like '孙_';

20250430145358628-image

比如:查询name为任意姓,名叫 乔 的学生记录。

代码:select * from students where name like '%乔';

20250430145610645-image

比如:查询name姓名中包含 白 的学生记录。

代码:select * from students where name like '%白%';

20250430145959680-image

查询name 姓名为两个字的学生记录

代码:select * from students where name like '__';

20250430150221911-image

查询name 姓 白 且年龄大于 30 的学上记录。

代码:select * from students where name like '白%%' and age > 30;

20250430150554188-image

4. 范围查询–in–between 开始值 and 结束值

  • in(值,值,值) 表示在一个非连续的范围内。

比如:查询hometown 家乡 是 北京 或 上海 或 广东 的学生记录。

代码:select * from students where hometown in ('北京','上海','广东');

20250430151209354-image

  • between 开始值 and 结束值 。表示在一个连续的范文内

比如:查询age 年龄 为 25 -30 的学生记录

代码:select * from students age between 25 and 30;

20250430151442892-image

查询 age 年龄 在20 或25 或30 的女生记录

代码:select * from students where age in (20,25,30) and sex = '女';

20250430151639933-image

查询age 年龄 25 到 30 以外的学生记录。

代码:select * from students age not between 25 and 30;

20250430152223739-image

5. 空判断

注意:null 与 ”是不同的

  • null:代表什么都没有;
  • ”:代表长度为 0 的字符串。
  • null不能用比较符去判断

判断空:is null (是否为null)

查询 card 身份证为 null 的学生记录。

代码:select * from students where card is null;

20250430152622911-image

判断非空:is not null (是否不为null)

查询card 身份证非 null 的学生记录。

代码:select * from students where card is not null;

20250430152839998-image

6. where 子句 在 与 updata 与 delete 语句中同样有效

将students表中 age 为 25,并且 name 为 孙尚香的学生 class 的班级修改 为2班

代码: update students set class = '2班' where age = 25 and name = '孙尚香';

20250430153828951-image

删除class 为 1班 ,并且 age 大于 30 的学生记录。

代码:delete from students where age > 30 and class = '1班';

20250430154409419-image

排序–asc升序–desc降序

  • 为了方便查看数据,可以对数据进行排序。
  • 语法
    • select * from 表名
      order by 字段 asc(升序)或者 desc(降序),字段 asc(升序) 或者 desc(降序),….
    • asc(默认值,可以省略):从小到大排序,即升序。
    • desc:从大到小排序,即降序。
  • 当一条selec语句出现了where条件和order by条件是,where条件需要在oredr by 条件的前面。
    • 例如:select * from 表名 where 条件 order by 字段;
  • 将行数据按照字段1进行排序,如果某些行字段1的值相同时,则按照字段2排序,以此类推。
    • 默认按照字段值从小到大排序

查询所有学生记录,按照age 年龄从小到大排序。

代码:select * from students order by age;

20250430164138819-image

查询所有学生记录,按照age 年龄从大到小排序,年龄相同时,再按students学号从小到大配许。

代码:select * from students order by age desc,students;

20250430164603832-image

查询所有男学生记录,按class班级从小打到排序,班级相同时,再按照students学号从大到小排序。

代码:select * from students where sex='男' order by class,students desc;

20250430165901886-image

聚合函数

  • 为了快速得到统计数据,经常会用到如下5个聚合函数。
    • 注意:聚合函数不能在 where 后面的条件中使用。

1. count() 总记录数

  • count(*):表示计算总记录数,括号中写 * 与 字段名,结果是相同的。

查询学生总数(学生总数就是students表中记录的总数)

select count(*) from students;
select count(nam) from students;

20250430170521388-image

查询性别sex 为女的学生总数。

select count(*) from students where sex='女';

20250430170708289-image

2. max最大值

  • max(字段)表示求此字段的最大值。

查询最大age 的年龄

select max(age) form students;

20250430170950572-image

查询性别sex为女 的最大 age 年龄。

select max(age) from students where sex='女';

20250430171144669-image

注意:

  • 聚合函数不能在 where 后面的条件中使用。
    • 比如:select * from students where age=max(age);
  • 聚合函数不能与普通字段同时出现在查询结果中。
    • 比如:select name,max(age) from students;

3. min最小值

  • min(字段):表示求此字段的最小值

比如:查询学生最小的age年龄

select min(age) from students;

20250430171533761-image

查询class班级为1班的最小age年龄,

select min(age) from students where class='1班';

20250430171702221-image

4. sum 求和

  • sum(字段):表示求此字段的和

查询学生age年龄的总和

select sum(age) from students;

20250430171845117-image

查询hometown字段中为北京的学生age年龄的总和

select sum(age) from students where hometown='北京';

20250430172031921-image

5. avg 平均值

  • avg(字段):表示求此字段的平均值。

查询学生平均 age 年龄

select avg(age) from students;

20250430172212699-image

查询sex性别为男 的平均age 年龄。

select avg(age) from students where sex='男';

20250430172343125-image

  • avg的字段中如果有null,null不做为分母计算平均。

案例

查询所有学生的最大年命、最小年龄、平均年龄

select max(age),min(age),avg(age) from students;

20250430172558167-image

查询class 1班 共有多少学生。

select count(*) from students where class='1班';

20250430174621885-image

查询 class 3班 中年龄小于 30 岁的有几个。

select count(*) from students where age < 30 and class='3班‘;

20250430173926826-image

数据分组 — group by 字段

1.分组

  • 按照字段分组,表示此字段相同的数据会被放到一个组中。
  • 分组的目的是配合聚合函数,聚合函数会对每一组的数据分别进行统计。
  • 语法
    • select 字段,字段,聚合函数,from 表名 group by 字段,字段;

比如:查询各种sex 性别的人数

select sex,count(*) from students group by sex;

20250430180107185-image

查询各种age 年龄的人数

select age,count(*) from students group by age;

20250430180316686-image

练习

用数组分组方法,统计各个班级学生总和,平均年龄,最大年龄,最小年龄。

select class,count(*), max(age),min(age),avg(age) from students group by class;

20250430181154950-image

统计各个班级学生总人数,平均年龄、最大年龄、最小年龄,但不统计三班,统计结果按照班级名称从大到小排序。

select class,count(*),max(age),min(age),avg(age) from students where class != '3班' group by class order by class desc;

20250430184335953-image

where 和 group by 和 order by 的顺序

  • select 字段 from 表名 where 条件 group by 字段 order by 字段 asc降序 或者 desc升序。

2. 分组后的数据筛选– having

  • 语法
    • select 字段,字段,聚合函数, from 表名 
      group by 字段,字段,字段,….
      having 字段,聚合函数…
  • having 后面的条件运算符与where 相同

比如:使用where 子句,查询男生总人数。

select  sex,count(*) from students where sex='男';

20250430181820197-image

使用having 子句,查询男生总人数

select sex,count(*) from students group by sex having sex='男';

20250430182120835-image

求班级人数大于三人的班级名字

select class,count(*) from students group by class having count(*) > 3;

20250430190127497-image

练习:

使用having子句,查询除了1班 以外,其它各个班级学生的平均年龄、最大年龄、最小年龄;

select class,count(*),max(age),min(age),avg(age) from students group by class having class != '1班';

20250430182702520-image

where 和 having 的区别

  • 对比where和having
    • where 是对from后面指定的表进行数据筛选,属于对原始数据的筛选。
      • where先筛选符合条件的记录,然后再聚合统计。
    • having是对group by 的结果进行筛选。
      • having先分组聚合统计,在统计的结果中筛选。
    • having后面的条件可以使用聚合函数,where后面的条件不可以使用聚合函数。

练习

查询班级总人数大于 2 人 的班级名称以及班级对应的总人数。

select class,count(*) from students group by class having count(*) > 2;

20250430190555520-image

查询平均年龄大于30岁的班级名称和班级总人数。

select class,count(*),avg(age) from students group by class having avg(age) >30;

20250430191126201-image

数据分页显示

1. 获取部分行–limit

当数据量过大时,在一页中查看数据是一件非常麻烦的事情。

  • 语法:limit 开始行,获取行数;
    • select 字段 from 表名 limit (start)开始行,(count)获取行数;
    • 从start代表开始,获取count多少条数据。
      • start索引从0开始,如省略start默认从0开始。
      • count代表要显示多少行。
    • limit总是出现在语句的最后。

查询 3行学生记录。

select * from students limit 0,3;

20250430192511305-image

省略start ,查询5行学生记录。

select * from students limit 5;

20250430192912510-image

练习

查询第五行开始的3条学生记录

select * from students limit 4,3;

20250430193108726-image

使用limit语句,查询年龄最大的学生记录。

select * from students order by age desc limit 1;

20250430193911690-image

查询年龄最小的女同学

select * from students where sex='女' order by age limit 1;

20250430194228972-image

2. 分页

当一张表记录特别多的时候,就需要用到分页显示。

已知:每页显示 m 条数据,求:查询第n页的数据。

  • 语法
    • select 字段 from 表名 limit (n-1)*m,m;
      • limit(n-1)*m:代表的是起始行的编号。
      • n:代表显示的页数。
      • m:代表的每页要显示多少记录
      • limit(n-1)*m,m

练习

每页显示4条记录,查询第3页的数据。

公式:

  • m=4
  • n=3
  • (n-1)*m = (3-1)*4 = 8
select * from students limit 8,4;

20250430194946405-image

每页显示4条记录,查询第二页的数据。

select * from students limit 4,4;

20250430195330942-image

查询students 表,每页显示5条记录,求总页数。

  • 查询记录总条数为a
  • 使用a除以每页显示条数5,得到b
  • 如果b为整数,则b为总数页。
  • 如果b不为整数,则b+1为总数页。

20250430200941518-image

20250430201236151-image

  • 11 / 5 = 2….(2+1)
  • 总页数为3
THE END
喜欢就支持一下吧
赞赏 分享