5-MySQL_表的增删改查

5-MySQL_表的增删改查 ✨✨ 欢迎大家来到小伞的大讲堂✨✨养成好习惯先赞后看哦~所属专栏MySQL小伞的主页xiaosan_bloggitee:许星让 (xu-xingrang) - Gitee.com制作不易点个赞吧谢谢喵CRUD : Create(创建), Retrieve(读取)Update(更新) Delete (删除)1. create语法INSERT [INTO] table_name [(column [, column] ...)] VALUES (value_list) [, (value_list)] ... value_list: value, [, value] ...案例创建一张学生表 mysql create table students( - id int unsigned primary key auto_increment, - sn int not null unique comment 学号, - name varchar(20) not null, - qq varchar(20) - );1.1 单行数据全列插入-- 插入两条记录value_list 数量必须和定义表的列的数量及顺序一致 -- 注意这里在插入的时候也可以不用指定id(当然那时候就需要明确插入数据到那些列了)那么mysql会使用默认的值进行自增。 mysql insert into students values(100,10000,张三,null); Query OK, 1 row affected (0.00 sec) mysql insert into students values(101,10001,李四,111111); Query OK, 1 row affected (0.00 sec) mysql select * from students; ---------------------------- | id | sn | name | qq | ---------------------------- | 100 | 10000 | 张三 | NULL | | 101 | 10001 | 李四 | 111111 | ---------------------------- 2 rows in set (0.01 sec)1.2 多行数据指定列插入-- 插入两条记录value_list 数量必须和指定列数量及顺序一致 mysql insert into students(id,sn,name) values(102,20001,曹孟德),(103,20002,孙仲谋); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql select * from students; ------------------------------- | id | sn | name | qq | ------------------------------- | 100 | 10000 | 张三 | NULL | | 101 | 10001 | 李四 | 111111 | | 102 | 20001 | 曹孟德 | NULL | | 103 | 20002 | 孙仲谋 | NULL | ------------------------------- 4 rows in set (0.00 sec)1.3 插入否则更新由于主键或者唯一键对应的值已经存在而导致插入失败mysql insert into students values(100,10010,刘玄德); ERROR 1136 (21S01): Column count doesnt match value count at row 1 mysql insert into students values(20001,刘玄德); ERROR 1136 (21S01): Column count doesnt match value count at row 1可以选择性的进行同步更新操作语法INSERT ... ON DUPLICATE KEY UPDATE column value [, column value] ...mysql insert into students(id,sn,name) values(102,20001,刘玄德) on duplicate key update sn 10010,name 刘玄德; Query OK, 2 rows affected (0.00 sec) -- 0 row affected: 表中有冲突数据但冲突数据的值和 update 的值相等 -- 1 row affected: 表中没有冲突数据数据被插入 -- 2 row affected: 表中有冲突数据并且数据已经被更新 -- 通过 MySQL 函数获取受到影响的数据行数 mysql select * from students; ------------------------------- | id | sn | name | qq | ------------------------------- | 100 | 10000 | 张三 | NULL | | 101 | 10001 | 李四 | 111111 | | 102 | 10010 | 刘玄德 | NULL | | 103 | 20002 | 孙仲谋 | NULL | ------------------------------- 4 rows in set (0.00 sec) mysql select row_count(); ------------- | row_count() | ------------- | 2 | ------------- 1 row in set (0.00 sec) -- ON DUPLICATE KEY 当发生重复key的时候1.4 替换-- 主键 或者 唯一键 没有冲突则直接插入 -- 主键 或者 唯一键 如果冲突则删除后再插入 mysql replace into students (sn,name) values (10001,曹阿瞒); Query OK, 2 rows affected (0.00 sec) -- 1 row affected: 表中没有冲突数据数据被插入 -- 2 row affected: 表中有冲突数据删除后重新插入2. Retrieve语法SELECT [DISTINCT] {* | {column [, column] ...} [FROM table_name] [WHERE ...] [ORDER BY column [ASC | DESC], ...] LIMIT ...案例mysql create table exam_result( - id int unsigned primary key auto_increment, - name varchar(20) not null comment 同学姓名, - chinese float default 0.0 comment 语文成绩, - math float default 0.0 comment 数学成绩, - english float default 0.0 comment 英语成绩); Query OK, 0 rows affected (0.01 sec) mysql insert into exam_result (name,chinese,math,english) values - (唐三藏, 67, 98, 56), - (孙悟空, 87, 78, 77), - (猪悟能, 88, 98, 90), - (曹孟德, 82, 84, 67), - (刘玄德, 55, 85, 45), - (孙权, 70, 73, 78), - (宋公明, 75, 65, 30); Query OK, 7 rows affected (0.00 sec) Records: 7 Duplicates: 0 Warnings: 02.1 select列2.1.1 全列查询-- 通常情况下不建议使用 * 进行全列查询 -- 1. 查询的列越多意味着需要传输的数据量越大 -- 2. 可能会影响到索引的使用。索引待后面课程讲解 mysql select * from exam_result; --------------------------------------- | id | name | chinese | math | english | --------------------------------------- | 1 | 唐三藏 | 67 | 98 | 56 | | 2 | 孙悟空 | 87 | 78 | 77 | | 3 | 猪悟能 | 88 | 98 | 90 | | 4 | 曹孟德 | 82 | 84 | 67 | | 5 | 刘玄德 | 55 | 85 | 45 | | 6 | 孙权 | 70 | 73 | 78 | | 7 | 宋公明 | 75 | 65 | 30 | --------------------------------------- 7 rows in set (0.00 sec)2.1.2 指定列查询-- 指定列的顺序不需要按定义表的顺序来 mysql select id,name,english from exam_result; ------------------------ | id | name | english | ------------------------ | 1 | 唐三藏 | 56 | | 2 | 孙悟空 | 77 | | 3 | 猪悟能 | 90 | | 4 | 曹孟德 | 67 | | 5 | 刘玄德 | 45 | | 6 | 孙权 | 78 | | 7 | 宋公明 | 30 | ------------------------ 7 rows in set (0.00 sec)2.1.3 查询字段为表达式-- 表达式包含一个字段 mysql select id,name,english10 from exam_result; --------------------------- | id | name | english10 | --------------------------- | 1 | 唐三藏 | 66 | | 2 | 孙悟空 | 87 | | 3 | 猪悟能 | 100 | | 4 | 曹孟德 | 77 | | 5 | 刘玄德 | 55 | | 6 | 孙权 | 88 | | 7 | 宋公明 | 40 | --------------------------- 7 rows in set (0.00 sec) -- 表达式包含多个字段 mysql select id,name,chinesemathenglish from exam_result; ------------------------------------- | id | name | chinesemathenglish | ------------------------------------- | 1 | 唐三藏 | 221 | | 2 | 孙悟空 | 242 | | 3 | 猪悟能 | 276 | | 4 | 曹孟德 | 233 | | 5 | 刘玄德 | 185 | | 6 | 孙权 | 221 | | 7 | 宋公明 | 170 | ------------------------------------- 7 rows in set (0.00 sec)2.1.4 未查询结果指定别名语法SELECT column [AS] alias_name [...] FROM table_name;mysql select id,name,chinesemathenglish total from exam_result; ---------------------- | id | name | total | ---------------------- | 1 | 唐三藏 | 221 | | 2 | 孙悟空 | 242 | | 3 | 猪悟能 | 276 | | 4 | 曹孟德 | 233 | | 5 | 刘玄德 | 185 | | 6 | 孙权 | 221 | | 7 | 宋公明 | 170 | ---------------------- 7 rows in set (0.00 sec)2.1.5 结果去重-- 98 分重复了 mysql select id,name,math from exam_result; --------------------- | id | name | math | --------------------- | 1 | 唐三藏 | 98 | | 2 | 孙悟空 | 78 | | 3 | 猪悟能 | 98 | | 4 | 曹孟德 | 84 | | 5 | 刘玄德 | 85 | | 6 | 孙权 | 73 | | 7 | 宋公明 | 65 | --------------------- 7 rows in set (0.00 sec)-- 去重结果 distinct mysql select distinct math from exam_result; ------ | math | ------ | 98 | | 78 | | 84 | | 85 | | 73 | | 65 | ------ 6 rows in set (0.00 sec)2.2 where条件比较运算符运算符说明, , , 大于大于等于小于小于等于等于NULL 不安全例如 NULL NULL 的结果是 NULL等于NULL 安全例如 NULL NULL 的结果是 TRUE(1)!, 不等于BETWEEN a0 AND a1范围匹配[a0, a1]如果 a0 value a1返回 TRUE(1)IN (option, ...)如果是 option 中的任意一个返回 TRUE(1)IS NULL是 NULLIS NOT NULL不是 NULLLIKE模糊匹配。% 表示任意多个包括 0 个任意字符_ 表示任意一个字符逻辑运算符运算符说明AND多个条件必须都为 TRUE(1)结果才是 TRUE(1)OR任意一个条件为 TRUE(1), 结果为 TRUE(1)NOT条件为 TRUE(1)结果为 FALSE(0)案例2.2.1 基本比较英语不及格的同学及英语成绩 ( 60 ) mysql select * from exam_result; --------------------------------------- | id | name | chinese | math | english | --------------------------------------- | 1 | 唐三藏 | 67 | 98 | 56 | | 2 | 孙悟空 | 87 | 78 | 77 | | 3 | 猪悟能 | 88 | 98 | 90 | | 4 | 曹孟德 | 82 | 84 | 67 | | 5 | 刘玄德 | 55 | 85 | 45 | | 6 | 孙权 | 70 | 73 | 78 | | 7 | 宋公明 | 75 | 65 | 30 | --------------------------------------- 7 rows in set (0.00 sec) mysql select name ,english from exam_result where english60; -------------------- | name | english | -------------------- | 唐三藏 | 56 | | 刘玄德 | 45 | | 宋公明 | 30 | -------------------- 3 rows in set (0.00 sec)2.2.2 使用and进行条件连接语文成绩在 [80, 90] 分的同学及语文成绩 mysql select name ,chinese from exam_result where chinese80 and chinese90; -------------------- | name | chinese | -------------------- | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | -------------------- 3 rows in set (0.00 sec)2.2.2 使用between and进行条件连接mysql select name ,chinese from exam_result where chinese between 80 and 90; -------------------- | name | chinese | -------------------- | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | -------------------- 3 rows in set (0.00 sec)2.2.3 使用 or 进行条件连接数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩 mysql select name ,math from exam_result where math 58 or math 59 or math 98 or math 99; ----------------- | name | math | ----------------- | 唐三藏 | 98 | | 猪悟能 | 98 | ----------------- 2 rows in set (0.00 sec)2.2.4 使用 in 进行条件连接mysql select name ,math from exam_result where math in (58,59,98,99); ----------------- | name | math | ----------------- | 唐三藏 | 98 | | 猪悟能 | 98 | ----------------- 2 rows in set (0.00 sec)2.2.5 % 匹配任意多个包括 0 个任意字符like模糊匹配mysql select name from exam_result where name like 孙%; ----------- | name | ----------- | 孙悟空 | | 孙权 | ----------- 2 rows in set (0.00 sec)_匹配严格一个字符mysql select name from exam_result where name like 孙_; -------- | name | -------- | 孙权 | -------- 1 row in set (0.00 sec)2.2.4 where 条件中比较运算符两侧都是字段语文成绩好于英语成绩的同学 mysql select name ,chinese,english from exam_result where chinese english; ----------------------------- | name | chinese | english | ----------------------------- | 唐三藏 | 67 | 56 | | 孙悟空 | 87 | 77 | | 曹孟德 | 82 | 67 | | 刘玄德 | 55 | 45 | | 宋公明 | 75 | 30 | ----------------------------- 5 rows in set (0.00 sec)2.2.5 where 条件中使用表达式mysql select name , chinese math english from exam_result where chinese math english 200; ------------------------------------- | name | chinese math english | ------------------------------------- | 刘玄德 | 185 | | 宋公明 | 170 | ------------------------------------- 2 rows in set (0.00 sec) mysql select name , chinese math english total from exam_result where chinese math english 200; ------------------ | name | total | ------------------ | 刘玄德 | 185 | | 宋公明 | 170 | ------------------ 2 rows in set (0.00 sec)由于mysql中表达式并非从左往右或者从右往左别名不能用在 where 条件中2.2.6 and与 not 的使用语文成绩 80 并且不姓孙的同学 mysql select name , chinese total from exam_result where chinese80 and name not like 孙%; ------------------ | name | total | ------------------ | 猪悟能 | 88 | | 曹孟德 | 82 | ------------------ 2 rows in set (0.00 sec)2.2.7 null 查询mysql select * from students; ----------------------------- | id | sn | name | qq | ----------------------------- | 100 | 10000 | 张三 | NULL | | 102 | 10010 | 刘玄德 | NULL | | 103 | 20002 | 孙仲谋 | NULL | | 104 | 10001 | 曹阿瞒 | NULL | ----------------------------- 4 rows in set (0.00 sec) mysql select name , qq from students where qq is null; ----------------- | name | qq | ----------------- | 张三 | NULL | | 刘玄德 | NULL | | 孙仲谋 | NULL | | 曹阿瞒 | NULL | ----------------- 4 rows in set (0.00 sec)2.2.8 null null 与 null null 的区别mysql select null null; ------------- | null null | ------------- | NULL | ------------- 1 row in set (0.00 sec) mysql select null null; --------------- | null null | --------------- | 1 | --------------- 1 row in set (0.00 sec)2.2.9 升序与降序查询同学各门成绩依次按 数学降序英语升序语文升序的方式显示 数学降序 mysql select name , math from exam_result order by math asc; ----------------- | name | math | ----------------- | 宋公明 | 65 | | 孙权 | 73 | | 孙悟空 | 78 | | 曹孟德 | 84 | | 刘玄德 | 85 | | 唐三藏 | 98 | | 猪悟能 | 98 | ----------------- 7 rows in set (0.00 sec) 数学升序 mysql select name , math from exam_result order by math desc; ----------------- | name | math | ----------------- | 唐三藏 | 98 | | 猪悟能 | 98 | | 刘玄德 | 85 | | 曹孟德 | 84 | | 孙悟空 | 78 | | 孙权 | 73 | | 宋公明 | 65 | ----------------- 7 rows in set (0.00 sec)order by 默认为升序(asc) 总分升序排序 mysql select name , math chinese english from exam_result order by math chinese math; ------------------------------------- | name | math chinese english | ------------------------------------- | 宋公明 | 170 | | 孙权 | 221 | | 刘玄德 | 185 | | 孙悟空 | 242 | | 曹孟德 | 233 | | 唐三藏 | 221 | | 猪悟能 | 276 | ------------------------------------- 7 rows in set (0.00 sec)order by 使用别名 mysql select name , math chinese english total from exam_result order by total; ------------------ | name | total | ------------------ | 宋公明 | 170 | | 刘玄德 | 185 | | 唐三藏 | 221 | | 孙权 | 221 | | 曹孟德 | 233 | | 孙悟空 | 242 | | 猪悟能 | 276 | ------------------ 7 rows in set (0.00 sec)2.2.10 where与order by结合查询姓孙的同学或者姓曹的同学数学成绩结果按数学成绩由高到低显示 mysql select name , math from exam_result where name like 孙% or name like 曹% order by math desc; ----------------- | name | math | ----------------- | 曹孟德 | 84 | | 孙悟空 | 78 | | 孙权 | 73 | ----------------- 3 rows in set (0.00 sec)2.3 筛选分页结果-- 起始下标为 0-- 从 0 开始筛选 n 条结果SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;-- 从 s 开始筛选 n 条结果SELECT ... FROM table_name [WHERE ...] [ORDER BY ...]LIMIT s, n;-- 从 s 开始筛选 n 条结果比第二种用法更明确建议使用SELECT ... FROM table_name [WHERE ...] [ORDER BY ...]LIMIT n OFFSET s;mysql select id ,name,math,english,chinese from exam_result order by id limit 7 offset 0; --------------------------------------- | id | name | math | english | chinese | --------------------------------------- | 1 | 唐三藏 | 98 | 56 | 67 | | 2 | 孙悟空 | 78 | 77 | 87 | | 3 | 猪悟能 | 98 | 90 | 88 | | 4 | 曹孟德 | 84 | 67 | 82 | | 5 | 刘玄德 | 85 | 45 | 55 | | 6 | 孙权 | 73 | 78 | 70 | | 7 | 宋公明 | 65 | 30 | 75 | --------------------------------------- 7 rows in set (0.00 sec)建议对未知表进行查询时最好加一条 LIMIT 1避免因为表中数据过大查询全表数据导致数据库卡死2.3. update语法:UPDATE table_name SET column expr [, column expr ...][WHERE ...] [ORDER BY ...] [LIMIT ...]案例:更新单列将孙悟空同学的数学成绩变更为 80 分 mysql select name ,math from exam_result where name 孙悟空; ----------------- | name | math | ----------------- | 孙悟空 | 78 | ----------------- 1 row in set (0.00 sec) mysql update exam_result set math 80 where name 孙悟空; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql select name , math from exam_result where name 孙悟空; ----------------- | name | math | ----------------- | 孙悟空 | 80 | ----------------- 1 row in set (0.00 sec)更新多列mysql update exam_result set math 60 ,chinese 70 where name 曹孟德; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql select name ,math ,chinese from exam_result where name 曹孟德; -------------------------- | name | math | chinese | -------------------------- | 曹孟德 | 60 | 70 | -------------------------- 1 row in set (0.00 sec)更新值为原值基础上变更mysql select name ,mathchineseenglish 总分 from exam_result order by 总分 limit 3; ------------------- | name | 总分 | ------------------- | 宋公明 | 170 | | 刘玄德 | 185 | | 曹孟德 | 197 | ------------------- 3 rows in set (0.00 sec) mysql select name, math, chinese math english 总分 FROM exam_result WHERE name IN (宋公明, 刘玄德, 曹孟德); ------------------------- | name | math | 总分 | ------------------------- | 曹孟德 | 90 | 227 | | 刘玄德 | 115 | 215 | | 宋公明 | 95 | 200 | ------------------------- 3 rows in set (0.00 sec) mysql select name, math, chinese math english 总分 FROM exam_result - ORDER BY 总分 LIMIT 3; ------------------------- | name | math | 总分 | ------------------------- | 宋公明 | 95 | 200 | | 刘玄德 | 115 | 215 | | 唐三藏 | 98 | 221 | ------------------------- 3 rows in set (0.00 sec)2.4 delete2.4.1 删除数据语法DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]案例mysql select * from exam_result where name 孙悟空; --------------------------------------- | id | name | chinese | math | english | --------------------------------------- | 2 | 孙悟空 | 87 | 80 | 77 | --------------------------------------- 1 row in set (0.00 sec) mysql delete from exam_result where name 孙悟空; Query OK, 1 row affected (0.00 sec) mysql select * from exam_result where name 孙悟空; Empty set (0.00 sec)2.4.1.1 删除整张表数据-incrementmysql create table for_delete( - id int primary key auto_increment, - name varchar(20) - ); Query OK, 0 rows affected (0.01 sec) mysql insert into for_delete(name) values(a),(b),(c); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql select * from for_delete; ---------- | id | name | ---------- | 1 | a | | 2 | b | | 3 | c | ---------- 3 rows in set (0.00 sec) mysql delete from for_delete; Query OK, 3 rows affected (0.00 sec) mysql select * from for_delete; Empty set (0.00 sec)注意我们删除了表数据,其中anto_increment在原值增长,并不会清零2.4.2 截断表语法TRUNCATE [TABLE] table_name注意这个操作慎用1. 只能对整表操作不能像 DELETE 一样针对部分数据操作2. 实际上MySQL 不对数据操作所以比 DELETE 更快但是TRUNCATE在删除数据的时候并不经过真正的事物所以无法回滚3.会重置 AUTO_INCREMENT 项mysql CREATE TABLE for_truncate ( - id INT PRIMARY KEY AUTO_INCREMENT, - name VARCHAR(20) - ); Query OK, 0 rows affected (0.00 sec) mysql INSERT INTO for_truncate (name) VALUES (A), (B), (C); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql select * from for_truncate; ---------- | id | name | ---------- | 1 | A | | 2 | B | | 3 | C | ---------- 3 rows in set (0.00 sec) -- 查看表结构会有 AUTO_INCREMENT2 项 SHOW CREATE TABLE for_truncate\G *************************** 1. row *************************** Table: for_truncate Create Table: CREATE TABLE for_truncate ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(20) DEFAULT NULL, PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT2 DEFAULT CHARSETutf8 1 row in set (0.00 sec)2.5 插入查询结果语法INSERT INTO table_name [(column [, column ...])] SELECT...案例删除表中的的重复复记录重复的数据只能有一份mysql create table duplicate_table(id int ,name varchar(20)); Query OK, 0 rows affected (0.01 sec) mysql INSERT INTO duplicate_table VALUES - (100, aaa), - (100, aaa), - (200, bbb), - (200, bbb), - (200, bbb), - (300, ccc); Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0-- 创建一张空表 no_duplicate_table结构和 duplicate_table 一样 mysql create table no_duplicate_table like duplicate_table; Query OK, 0 rows affected (0.00 sec) -- 将 duplicate_table 的去重数据插入到 no_duplicate_table mysql insert into no_duplicate_table select distinct * from duplicate_table; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 -- 通过重命名表实现原子的去重操作 mysql rename table duplicate_table to old_duplicate_table; Query OK, 0 rows affected (0.00 sec) mysql rename table no_duplicate_table to duplicate_table; Query OK, 0 rows affected (0.00 sec) -- 查看最终结果 mysql select * from duplicate_table; ------------ | id | name | ------------ | 100 | aaa | | 200 | bbb | | 300 | ccc | ------------ 3 rows in set (0.00 sec)3. 聚合函数函数说明COUNT([DISTINCT] expr)返回查询到的数据的 数量SUM([DISTINCT] expr)返回查询到的数据的 总和不是数字没有意义AVG([DISTINCT] expr)返回查询到的数据的 平均值不是数字没有意义MAX([DISTINCT] expr)返回查询到的数据的 最大值不是数字没有意义MIN([DISTINCT] expr)返回查询到的数据的 最小值不是数字没有意义3.1 统计查询数据的个数mysql select count(*) from students; ---------- | count(*) | ---------- | 4 | ---------- 1 row in set (0.00 sec) mysql select count(1) from students; ---------- | count(1) | ---------- | 4 | ---------- 1 row in set (0.00 sec) count(*)与count(1) 等价3.1.1 null不会计入统计mysql select count(qq) from students; ----------- | count(qq) | ----------- | 0 | ----------- 1 row in set (0.00 sec) mysql select * from students; ----------------------------- | id | sn | name | qq | ----------------------------- | 100 | 10000 | 张三 | NULL | | 102 | 10010 | 刘玄德 | NULL | | 103 | 20002 | 孙仲谋 | NULL | | 104 | 10001 | 曹阿瞒 | NULL | ----------------------------- 4 rows in set (0.00 sec)3.1.2 count()统计的是全部数据并非去重数据个数非去重成绩数量 mysql select count(math) from exam_result; ------------- | count(math) | ------------- | 6 | ------------- 1 row in set (0.00 sec) mysql select math from exam_result; ------ | math | ------ | 98 | | 98 | | 90 | | 115 | | 73 | | 95 | ------ 6 rows in set (0.00 sec) 去重成绩数量 mysql select count(distinct math) from exam_result; ---------------------- | count(distinct math) | ---------------------- | 5 | ---------------------- 1 row in set (0.00 sec)3.2 sum查询数据总和mysql select sum(math) from exam_result; ----------- | sum(math) | ----------- | 569 | ----------- 1 row in set (0.00 sec) -- 不及格 60 的总分没有结果返回 NULL mysql select sum(math) from exam_result where math 60; ----------- | sum(math) | ----------- | NULL | ----------- 1 row in set (0.00 sec)3.3 查询数据平均值mysql select avg(mathchineseenglish) from exam_result; --------------------------- | avg(mathchineseenglish) | --------------------------- | 226.66666666666666 | --------------------------- 1 row in set (0.00 sec)3.4 查询数据最大值mysql select max(english) from exam_result; -------------- | max(english) | -------------- | 90 | -------------- 1 row in set (0.00 sec) mysql select english from exam_result; --------- | english | --------- | 56 | | 90 | | 67 | | 45 | | 78 | | 30 | --------- 6 rows in set (0.00 sec)3.4 查询数据最小值mysql select min(english) from exam_result; -------------- | min(english) | -------------- | 30 | -------------- 1 row in set (0.00 sec)