【MySQL】基本查询(二)

文章目录

  • [一. 结果排序](#一. 结果排序)
  • [二. 筛选分页结果](#二. 筛选分页结果)
  • [三. Update](#三. Update)
  • [四. Delete](#四. Delete)
  • [五. 截断表](#五. 截断表)
  • [六. 插入查询结果](#六. 插入查询结果)
  • 结束语

操作如下表

sql 复制代码
//创建表结构
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 '英语成绩'
    -> );


//插入测试数据
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);

一. 结果排序

语法:

ASC 升序(从小到大)

DESX 降序(从大到小)

排序默认为ASC 升序
select ... from table_name order by 属性 [ASC 或者 DESC]

注意:没有order by 子句的查询,返回的顺序是未定义的。

NULL值比任何值都小


查询同学及数学成绩,按数学成绩升序显示

sql 复制代码
mysql> select name,math from exam_result order by math;
+-----------+------+
| name      | math |
+-----------+------+
| 宋公明    |   65 |
| 孙权      |   73 |
| 孙悟空    |   78 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+

查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示

多字段排序,排序优先级随书写顺序

sql 复制代码
mysql> select name,math,english,chinese from exam_result order by math desc,english asc,math asc;
+-----------+------+---------+---------+
| name      | math | english | chinese |
+-----------+------+---------+---------+
| 唐三藏    |   98 |      56 |      67 |
| 猪悟能    |   98 |      90 |      88 |
| 刘玄德    |   85 |      45 |      55 |
| 曹孟德    |   84 |      67 |      82 |
| 孙悟空    |   78 |      77 |      87 |
| 孙权      |   73 |      78 |      70 |
| 宋公明    |   65 |      30 |      75 |
+-----------+------+---------+---------+

查询同学及总分,由高到低

order by 子句可以使用别名

sql 复制代码
mysql> select name,chinese+math+english as total from exam_result order by total desc;
+-----------+-------+
| name      | total |
+-----------+-------+
| 猪悟能    |   276 |
| 孙悟空    |   242 |
| 曹孟德    |   233 |
| 唐三藏    |   221 |
| 孙权      |   221 |
| 刘玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+

查询姓孙的同学或者姓曹的同学的数学成绩,结果按数学成绩由高到低显示

sql 复制代码
mysql> select name,math from exam_result where name like '孙%' or name like '曹%' order by math desc;
+-----------+------+
| name      | math |
+-----------+------+
| 曹孟德    |   84 |
| 孙悟空    |   78 |
| 孙权      |   73 |
+-----------+------+

二. 筛选分页结果

行的起始下标是0

从0开始,筛选n条结果(默认从0开始)
select ... from table_name ... limit n

从s开始,筛选n条结果
select ... from table_name ... limit s,n

从s开始,筛选n条结果(建议使用
select ... from table_name ... limit n offset s

注意:对未知表进行查询时,最好加一条limit 1,避免因为表中数据过大,查询全表数据导致数据库卡死


按id进行分页,每页3条记录,分别显示第1,2,3页

sql 复制代码
//第一页
mysql> select id,name,math,english,chinese from exam_result
order by id limit 3 offset 0;
+----+-----------+------+---------+---------+
| id | name      | math | english | chinese |
+----+-----------+------+---------+---------+
|  1 | 唐三藏    |   98 |      56 |      67 |
|  2 | 孙悟空    |   78 |      77 |      87 |
|  3 | 猪悟能    |   98 |      90 |      88 |
+----+-----------+------+---------+---------+
//第二页
mysql> select id,name,math,english,chinese from exam_result
order by id limit 3 offset 3;
+----+-----------+------+---------+---------+
| id | name      | math | english | chinese |
+----+-----------+------+---------+---------+
|  4 | 曹孟德    |   84 |      67 |      82 |
|  5 | 刘玄德    |   85 |      45 |      55 |
|  6 | 孙权      |   73 |      78 |      70 |
+----+-----------+------+---------+---------+
//第三页
mysql> select id,name,math,english,chinese from exam_result
order by id limit 3 offset 6;
+----+-----------+------+---------+---------+
| id | name      | math | english | chinese |
+----+-----------+------+---------+---------+
|  7 | 宋公明    |   65 |      30 |      75 |
+----+-----------+------+---------+---------+

三. Update

update table_name set 属性=数据 [where...] [order by ...] [limit ...]

\]内的容可以省略

将孙悟空同学的数学成绩变更为80分

sql 复制代码
mysql> update exam_result set math=80 where name='孙悟空';

将曹孟德同学的数学成绩变更为60分,语文成绩变更为70分

sql 复制代码
mysql> update exam_result set math=60,chinese=70 where name='曹孟德';

将总成绩倒数前三的3位同学的数学成绩加上30分

MySQL不支持math+=30,但可以使用math=math+30

sql 复制代码
mysql> update exam_result set math=math+30 order by english+math+chinese limit 3;

将所有同学的语文成绩更新为原来的2倍

sql 复制代码
没有where子句的update会更新全表数据
update exam_result set chinese=chinese*2;

四. Delete

delete from table_name [where...] [order by...] [limit ...]

删除孙悟空同学的信息

sql 复制代码
delete from exam_result where name='孙悟空';

删除整表数据 注意慎用

delete from table_name;

需要注意的是,delete删除数据不会改变auto_increment自增长的记录

五. 截断表

truncate [table] table_name;

注意:此操作慎用

  1. 只能对整表操作,不能像delete那样针对部分数据使用
  2. 实际上truncate不对数据操作,所以比delete更快,但是truncate在删除数据的时候,并不经过真正的事物,所以无法回滚
  3. 会重置auto_increment

六. 插入查询结果

insert into table_name 属性列 select ...

将select查询的结果插入表中

实验:删除表中的重复数据,重复数据只能有一份

sql 复制代码
mysql> create table duplicate_table(
    -> id int,
    -> name varchar(20)
    -> );
mysql> insert into duplicate_table values
    -> (100,'aaa'),
    -> (100,'aaa'),
    -> (200,'bbb'),
    -> (200,'bbb'),
    -> (200,'bbb'),
    -> (300,'ccc');

思路:建立一个具有相同表结构的表,然后将dupliate_table的数据,无重复的查找出来,然后插入新表中,再将新表重命名为duplicate_table

sql 复制代码
//建立新表
mysql> create table no_duplicate_table like duplicate_table;
//将查询的无重复的数据插入新表
mysql> insert into no_duplicate_table select distinct * from duplicate_table;
//删除旧表
mysql> drop table duplicate_table;
//重命名新表
mysql> rename table no_duplicate_table to duplicate_table;

结束语

感谢你的阅读

如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。

相关推荐
a9511416421 分钟前
c++如何解析二进制协议中的可选字段读取逻辑及其反序列化【详解】
jvm·数据库·python
weixin_5806140013 分钟前
golang如何实现时间格式化_golang时间格式化方法详解
jvm·数据库·python
forEverPlume14 分钟前
c++怎么利用std--span实现在不拷贝数据的前提下解析大规模文件【进阶】
jvm·数据库·python
FinTech老王20 分钟前
逻辑删除不等于物理销毁:KingbaseES敏感数据标记与销毁实操指南
数据库·安全·oracle
HHHHH1010HHHHH24 分钟前
Tailwind CSS如何快速定义固定宽高比_使用aspect-square实现CSS正方形
jvm·数据库·python
梦想的旅途226 分钟前
解构自动化办公新思路:实现外部群聊能力的深度集成与交互
java·数据库·rpa
m0_5150984228 分钟前
c++怎么获取文件的Inode节点信息_stat结构体深度解析【详解】
jvm·数据库·python
m0_6742946435 分钟前
HTML怎么限制输入字符数_HTML input maxlength属性用法【详解】
jvm·数据库·python
maqr_11041 分钟前
layui table单元格编辑 layui表格如何实现可编辑
jvm·数据库·python
z4424753261 小时前
HTML函数开发用旋转屏有优势吗_特殊硬件形态适配说明【方法】
jvm·数据库·python