当前位置:网站首页>入门MySql表的增删查改
入门MySql表的增删查改
2022-08-04 11:00:00 【指挥部在下面】
1.新增
insert into 表名 values
(值,值……),
(值,值……),
(值,值……);
实例:
1.全列插入
mysql> insert into student values
-> (60,'小明'),
-> (58,'保罗');
2.指定列插入
mysql> insert into student (age) values
-> (50);
2.查询
1.全列查询
select * from 表名;
2.指定列查询
我们创建一个成绩表
create table exam_result(
id int,
name varchar(20),
chinese decimal(3,1),
math decimal(3,1),
english decimal(3,1)
);
insert into exam_result values
(1,"唐三藏",67,98,56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98.5, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘玄德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);
select 列名,列名,……from 表名;
select id,name,chinese from exam_result;
3.查询字段为表达式
select *,chinese+math+english from exam_result;
4.我们发现上面表格最后一列太长,不美观,那么我们就可以用别名 as.
select *,chinese+math+english as total from exam_result;
5.去重,distinct
我们发现上面表格里面,math有2个98.0,我们可以使用distinct去重
select distinct math from exam_result;
6.使用order by 排序
asc -- 排序 (可以不写)
desc -- 降序
NULL 数据排序,视为比任何值都小,升序出现在最上面,降序出现在最下面
select * from exam_result order by chinese; -- 按语文成绩升序
select * from exam_result order by chinese desc; -- 按语文成绩降序
可以对多个字段进行排序,排序优先级按书写顺序。
select * from exam_result order by chinese desc,math asc,english asc;
-- 按照语文降序,数学升序,英语升序
7.使用where进行条件查询
比较运算符:
运算符 | 说明 |
>,>=,<,<= | |
= | Null不安全,null=null的结果是null |
<=> | 等于,null是安全,null=null的结果是true |
!=,<> | 不等于 |
between a and b | 如果a<=value<=b,返回true |
in(option,……) | 如果是option中等任意一个,返回true |
is null | |
is not null | |
like | 模糊匹配。% |
逻辑运算符:
运算符 | 说明 |
and | 全真才是真 |
or | 一真就是真 |
not |
注意:1.where可以使用表达式,但是不能使用别名
2.and优先级高于or,可以使用()包裹优先执行的语句
1.基本查询
-- 查询总分在200以下的同学
select name,chinese+math+english as 总分 from result where chinese+math+english <200;
2.and和or的优先级
select * from exam_result where chinese>80 or math>70 and english>70;
select * from exam_result where (chinese>80 or math>70) and english>70;
第二个语句是一定要满足english>70
3.范围查询
between....and....
select * from exam_result where chinese between 80 and 90;-- 语文成绩在[80,90]
补充:其实使用and也可以实现,但是between and 的效率更高。
in
select * from exam_result where chinese in(88,70,87.5);-- 查询语文成绩是88,70,87.5
4.使用like模糊查询
%匹配任意多个(包括0个)字符
_:一个下划线匹配一个字符
8.使用limit进行分页查询
语法
select ....from 表名 [where....] [order by .....] limit n offset s; -- 从s开始,查询n条记录
功能实现:按id分页,每页3条记录。
之前一共插入7条记录,第三页只有一条记录
修改(update)
语法
update 表名 set 列名 = 值[where...] [order by.....] [limit....];
功能实现:将孙悟空的语文成绩改成90,数学成绩改成88
删除(delete)
delete from 表名 [where...] [order by....] [limit....offset...];
边栏推荐
- [easyUI]修改datagrid表格中的值
- 《迁移学习导论》第2版,升级内容抢先看!
- Win11 file types, how to change?Win11 modify the file suffix
- 音频编辑 合唱
- Learn to use the basic interface of set and map
- winform 在Datatable插入一笔数据
- MySQL:完整性约束和 表的设计原则
- There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
- 职责链模式(responsibilitychain)
- MATLAB程序设计与应用 3.2 矩阵变换
猜你喜欢
随机推荐
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
Difference between ArrayList and LinkedList
《迁移学习导论》第2版,升级内容抢先看!
强烈推荐一款优秀且通用的后台管理系统
【机器学习】:如何对你的数据进行分类?
Heap Sort
What is the principle of thermal imaging temperature measurement?Do you know?
MySQL核心SQL:结构化查询语句SQL、库操作、表操作、CRUD
Business collocations
ArrayList和LinkedList的区别
OD-Model【5】:YOLOv1
Mysql——》类型转换符binary
数据化管理洞悉零售及电子商务运营——零售密码
开源一夏|ArkUI如何自定义弹窗(eTS)
航企纠缠A350安全问题 空客主动取消飞机订单
BOSS 直聘回应女大学生连遭两次性骚扰:高度重视求职者安全,可通过 App 等举报
RL78开发环境
Camunda overall architecture and related concepts
章节小测一
在 .NET MAUI 中如何更好地自定义控件