当前位置: 首页 > news >正文

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

表的增删查改

  • 一.Create
    • 1.单行数据,全列插入
    • 2.单行数据,指定列插入
    • 3.多行数据插入
    • 4.插入,若冲突则更新
    • 5.替换
  • 二.Retrieve
    • 1.select 列
    • 2.where 子句
      • 运算符
    • 3.order by 子句
    • 4.筛选分页结果
  • 三.Update
  • 四.Delete
    • 1.删除条目
    • 2.截断表
  • 五.插入查询的数据
  • 六.聚合函数
  • 七.分组查询——group by 子句
  • 八.SQL 查询中各个关键词的执行先后顺序

一.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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Git协作
  • 预测算法面试
  • 【ARMv8/v9 GIC 系列 5.6 -- GIC 超优先级中断详细介绍】
  • mysql的事务,你弄懂了吗?(Innodb)
  • R语言学习笔记3-基本类型篇
  • DR模式介绍
  • C++20中的基于范围的for循环(range-based for loop)
  • leetcode165.解密数字
  • 矩阵分析与应用1-矩阵代数基础
  • 热词分析与维度人物构建
  • HarmonyOS Next应用开发之系统概述
  • Flink 提交作业的方式
  • dataX入门
  • 科研绘图系列:R语言双侧条形图(bar Plot)
  • STM32的独立看门狗详解
  • 77. Combinations
  • Docker容器管理
  • EOS是什么
  • Java编程基础24——递归练习
  • Java超时控制的实现
  • Linux Process Manage
  • node 版本过低
  • PHP面试之三:MySQL数据库
  • Zepto.js源码学习之二
  • 阿里云Kubernetes容器服务上体验Knative
  • 从PHP迁移至Golang - 基础篇
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 用Python写一份独特的元宵节祝福
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • ​用户画像从0到100的构建思路
  • $LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
  • $refs 、$nextTic、动态组件、name的使用
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (Java入门)学生管理系统
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (四)c52学习之旅-流水LED灯
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • ***利用Ms05002溢出找“肉鸡
  • **PHP二维数组遍历时同时赋值
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .Net Core 微服务之Consul(三)-KV存储分布式锁
  • .NET Project Open Day(2011.11.13)
  • .NET6 命令行启动及发布单个Exe文件
  • .NET6实现破解Modbus poll点表配置文件
  • .Net的C#语言取月份数值对应的MonthName值
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .NET下的多线程编程—1-线程机制概述
  • .NET中的十进制浮点类型,徐汇区网站设计
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • @Autowired和@Resource装配
  • @DateTimeFormat 和 @JsonFormat 注解详解