【MySQL】6.表的增删查改(CURD)

一.Create

create table students(
id int unsigned primary key auto_increment,
sn int unsigned unique key,
name varchar(20) not null,
qq varchar(32) unique key
);

1.单行数据,全列插入

insert into students values(1, 123, '张飞', '45678');

2.单行数据,指定列插入

insert into students (sn, name, qq) values(234, '关羽', '23456');

3.多行数据插入

insert into students values (12, 126, '曹操', '12345'), (13, 127, '许攸', '34567');
insert into students (sn, name, qq) values (128, '孙权', '66666'), (129, '许攸', '88888');

4.插入,若冲突则更新

insert into students values (13, 130, '曹操', '111111')  on duplicate key update sn=130, name='曹操', qq='111111'
  • – 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
  • – 1 row affected: 表中没有冲突数据,数据被插入
  • – 2 row affected: 表中有冲突数据,并且数据已经被更新

5.替换

//主键或者唯一键没有冲突,则直接插入; 
//主键或者唯一键如果冲突,则删除后再插入
replace into students (sn, name, qq) vlaues (20001, '曹阿瞒', '111222');
  • 1 row affected: 表中没有冲突数据,数据被插入
  • 2 row affected: 表中有冲突数据,删除后重新插入

二.Retrieve

create table exam_result(
id int unsigned primary key auto_increment,
name varchar(20) not null,
chinese float default 0.0,
math float default 0.0,
english float default 0.0
);

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);

1.select 列

//全列查询
select * from exam_result;

//指定列查询
select id from exam_result;
select id, math, name from exam_result;

//查询字段为表达式
select id, name, math+chinese+english as total;

+-----------+-------+
| name      | total |
+-----------+-------+
| 唐三藏    |   221 |
| 孙悟空    |   242 |
| 猪悟能    |   276 |
| 曹孟德    |   233 |
| 刘玄德    |   185 |
| 孙权      |   221 |
| 宋公明    |   170 |
+-----------+-------+
7 rows in set (0.00 sec)

//结果去重
select distinct math from exam_result;

2.where 子句

运算符

比较运算符 说明
>, >=, <, <= 大于,大于等于,小于,小于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL,无法比较
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <> 不等于,NULL 不安全,例如 NULL != NULL 和 NULL<>NULL 的结果都是 NULL
between a0 and a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
in(option, …) 如果是 option 中的任意一个,返回 TRUE(1)
is NULL 是 NULL
is not NULL 不是 NULL
is not NULL 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符
逻辑运算符 说明
and 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
or 任何一个条件为 TRUE(1),结果就是 TRUE(1)
not 条件真假取反
//1.查找英语成绩小于60的同学名字和英语成绩
select name, english from exam_result where english<60;

//2.查找语文成绩在[80,90]的同学名字和语文成绩
select name, chinese from exam_result where chinese>=80 and chinese<=90;

//3.查找数学成绩是58或者59或者98或者99的同学名字和数学成绩
select name, math from exam_result where math=58 or math=59 or math=98 or math=99;
//更优雅的写法
select name, math from exam_result where math in (58, 59, 98, 99);

//4.查找姓孙的同学的名字
select name from exam_result where where name like '孙%';

+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+

//5.查找孙某同学的名字
select name from exam_result where name like '孙_';

+--------+
| name   |
+--------+
| 孙权   |
+--------+

//6.查找总分在200以下的同学的名字,总分
select name, chinese+math+english as total from exam_result where chinese+math+english<200;
//这样写可以吗?不可以!!!因为先执行where子句,然后才执行select,对总分重命名
//select name, chinese+math+english as total from exam_result where total<200;

//7.查找语文成绩大于80并且不姓孙的同学的名字,语文成绩
select name, chinese from exam_result where chinese>80 and name not like '孙%';

//8.查找孙某同学,否则要求总成绩>200并且语文成绩<数学成绩并且英语成绩>80,
//这些同学的名字,各科成绩和总成绩
select name, chinese, math, english, chinese+math+english as total from esam_result
where (name like '孙_') or (chinese+math+english>200 and chinese<math and english>80);

3.order by 子句

order by 子句用于对查询到的结果排序,如果没有 order by 子句,结果的顺序是未定义的,永远不要依赖于这个顺序
顺序选择:
asc(ascdending)——升序
desc(descending)——降序
默认是 asc

注意:NULL 无法和其它值比较,升序会排在最前面,降序会排在最后面

//1.查找所有同学的名字和数学成绩,结果按数学成绩升序显示
select name, math from exam_result order by math asc;

//2.查询所有同学的名字和各门成绩,依次按数学降序,英语升序,英语升序显示(若前面条件相等则依次往后比较)
select name, math, chinese, english from exam_result 
order by math desc, english asc, chinese asc;

//3.查询所有同学的名字和总分,结果按总分升序显示
select name, math+chinese+english as total from exam_result order by math+chinese+english asc;
//可以这么写吗?可以!!!因为是先把结果select出来,然后再执行order by排序
select name, math+chinese+english as total from exam_result order by total asc;

//4.查询姓孙或姓曹的同学的名字和数学成绩,结果按数学成绩降序显示
select name, math from exam_result where name like '孙%' or name like '曹_' order by math desc;

4.筛选分页结果

按照需求显示部分的结果

select * from exam_result limit 3; //从起始行开始,连续显示3行

select * from exam_result limit 2, 4; //从第2行(第0行是起始行),连续显示4行

select * from exam_result limit 4 offset 2; //从第2行(第0行是起始行),连续显示4行

三.Update

update tb_name set column=expr [, column=expr …] [where …] [order by …] [limit …]
一般来说,update 通常要搭配 where 子句使用,否则就是对所有条目都更新。也就是说,update 的前置动作是查找

//1.将孙悟空的数学成绩变更为80分
update exam_result set math=80 where name='孙悟空';

//2.将曹孟德的数学成绩改成60,语文成绩改成70
update exam_result set math=60, chinese=70 where name='曹孟德';

//3.将总成绩倒数前三的3位同学的数学成绩加上30分
update exam_result set math=math+30 order by chinese+math+english asc limit 3;

//4.将所有同学的语文成绩更新为原来的两倍
update exam_result set chinese=chinese*2;

四.Delete

1.删除条目

delete from tb_name [where …] [order by …] [limit …]
和 update 一样,delete 的通常查找的前置动作

//1.删除孙悟空同学的考试信息
delete from exam_result where name='孙悟空';

//2.删除总分最低同学的考试信息
delete from exam_result order by math+chinese+english asc limit 1;

//清空表(删除所有条目)
delete from exam_result;

2.截断表

truncate exam_result;

delete 和 truncate 都会清空表,但是 truncate 会将 auto_increment 计数器重置。(还有其它方面区别,后面讲事务时再提)

五.插入查询的数据

案例:将表 duplicate_table 去重

//第一步:创建一个和duplicate_table结构一样的表no_duplicate_table
create table no_duplicate_table like duplicate_table;

//第二步:将去重查询duplicate_table的结果插入到no_duplicate_table中
insert into no_duplicate_table select distinct * from duplicate_table;

//第三步:将duplicate_table改成其它名字,将no_duplicate_table重命名为duplicate_table
alter table duplicate_table rename to old_duplicate_table;
alter table no_duplicate_table rename to duplicate_table;

六.聚合函数

所谓的聚合,就是把查询到的多行数据聚在一起,形成一行

函数 说明
count 返回查询到的数据的数量
sum 返回查询到的数据的总和,只对数字有意义
avg 返回查询到的数据的平均数,只对数字有意义
max 返回查询到的数据的平均数,只对数字有意义
min 返回查询到的数据的最小值,只对数字有意义
//1.统计班级有多少名同学
select count(*) from exam_result; //相当于统计条目的个数

+----------+
| count(*) |
+----------+
|        7 |
+----------+

//也可以像下面这样写
select count(1) from exam_result;

+----------+
| count(1) |
+----------+
|        7 |
+----------+

//原因在于执行select 1 from exam_result, 效果如下:
+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
+---+

//2.统计数学成绩有多少个
select count(math) from exam_result;

 +-------------+
| count(math) |
+-------------+
|           7 |
+-------------+

//3.统计有多少个不同的数学成绩
select count(distinct math) from exam_result;

//4.统计数学成绩的总分
select sum(math) from exam_result;

//5.统计数学成绩平均分
select sum(math)/count(*) from exam_result;
或者
select avg(math) from exam_result;

//6.统计英语成绩不及格的人数
select count(*) from exam_result where english<60;

//7.统计总成绩的平均分
select avg(math+chinese+english) from exam_result;

//可以这样写吗?不能!!!max(math)是math这一列综合计算得出的结果,而每个条目的name都不同,不能压缩成1个
//语法上不允许将它们放一起
//select name, max(math) from exam_result;
select max(math), min(math) form exam_result; //这样写是可以的,

七.分组查询——group by 子句

分组的目的方便聚合统计。
在 select 中使用 group by 子句,按照指定列分组,然后查询。
格式:select column1, column2, … from table group by column;

案例:
准备工作,创建一个雇员信息表(来自 oracle 9i 的经典测试表)

  • EMP 员工表
  • DEPT 部门表
  • SALGRADE 工资等级表
//1.显示每个部门的平均工资和最高工资
select deptno, avg(sal), max(sal) from EMP group by deptno; 
//deptno相同的条目会分成一组
//这里deptno和聚合函数能放在一起,因为同一组内的deptno是相同的,可以压缩成1个

//2.显示每个部门每种岗位的平均工资和最低工资
select deptno, job, avg(sal), min(sal) group by deptno, job;
//deptno, job相同的条目会分成一组

//3.显示平均工资低于2000的部门和它的平均工资
select deptno, avg(sal) as deptavg from EMP group by deptno having deptavg < 2000;

//说明:having是对聚合后的统计数据,进行条件筛选,通常和group by搭配使用

having VS where
having 和 where 都能做条件筛选,但它们的应用场景完全不同:

  1. where 对具体的任意列进行条件筛选
  2. having 对分组之后的结果进行条件筛选
    本质是因为他们条件筛选的阶段不同。

八.SQL 查询中各个关键词的执行先后顺序

SQL 查询中各个关键字的执行先后顺序:
from > on> join > where > group by > with > having > select > distinct > order by > limit

相关推荐

  1. MySQL6.增删(CURD)

    2024-07-12 09:18:06       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 09:18:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 09:18:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 09:18:06       58 阅读
  4. Python语言-面向对象

    2024-07-12 09:18:06       69 阅读

热门阅读

  1. 开源项目的机遇与挑战

    2024-07-12 09:18:06       25 阅读
  2. 从0到1搭建数据中台(2):数据中台架构

    2024-07-12 09:18:06       25 阅读
  3. 【C/C++】内存相关

    2024-07-12 09:18:06       26 阅读
  4. 【LeetCode 0169】【摩尔投票算法】主元素

    2024-07-12 09:18:06       25 阅读
  5. 每日一道算法题 LCR 151. 彩灯装饰记录 III

    2024-07-12 09:18:06       31 阅读
  6. 【随想】社交

    2024-07-12 09:18:06       23 阅读
  7. 谷歌独立站:纯净网络空间,自由与创新的融合

    2024-07-12 09:18:06       27 阅读
  8. Centos解决服务器时间不准的问题

    2024-07-12 09:18:06       24 阅读
  9. 计算机视觉发展历史、优势以及面临的挑战

    2024-07-12 09:18:06       20 阅读
  10. 使用bsdconfig配置CBSD NAT

    2024-07-12 09:18:06       25 阅读