当前位置:网站首页>入门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...];
边栏推荐
- SkiaSharp 之 WPF 自绘 粒子花园(案例版)
- RAID介绍及RAID5配置实例
- Doing Homework HDU - 1074
- [Hongke case] Assembling furniture based on 3D camera
- 解析treeSet集合进行自定义类的排序
- Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
- Difference between ArrayList and LinkedList
- 深度强化学习与APS的一些感想
- Small program containers accelerate the construction of an integrated online government service platform
- 小程序容器加快一体化在线政务服务平台建设
猜你喜欢

What is the principle of thermal imaging temperature measurement?Do you know?

MySQL core SQL: SQL structured query statements, library, table operation, CRUD

【Inspirational】The importance of review

AWS Lambda related concepts and implementation approach

JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题

数字知识库及考学一体化平台

MATLAB程序设计与应用 3.1 特殊矩阵

中介者模式(Mediator)

C语言*小白的探险历程

【虹科案例】基于3D相机组装家具
随机推荐
关于架构的思考
Maple 2022软件安装包下载及安装教程
少即是多:视觉SLAM的点稀疏化(IROS 2022)
Why are all hotel bathrooms transparent?
使用.NET简单实现一个Redis的高性能克隆版(二)
helm安装
【LeetCode】701.二叉搜索树中的插入操作
Maple 2022 software installation package download and installation tutorial
datax oracle to oracle incremental synchronization
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
【Inspirational】The importance of review
八、MFC对话框
利用pytest hook函数实现自动化测试结果推送企业微信
临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
audio_policy_configuration.xml配置文件详解
学会使用set和map的基本接口
Rust 入门指南 (用 WASM 开发第一个 Web 页面)
MySQL最大建议行数2000w, 靠谱吗?
AWS Lambda related concepts and implementation approach
