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

结束语

感谢你的阅读

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

相关推荐
异世界贤狼转生码农2 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong2 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪2 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
冷崖7 小时前
MySQL异步连接池的学习(五)
学习·mysql
时序数据说7 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.11 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)11 小时前
KINGBASE集群日常维护管理命令总结
数据库·database
妖灵翎幺11 小时前
Java应届生求职八股(2)---Mysql篇
数据库·mysql
HMBBLOVEPDX11 小时前
MySQL的事务日志:
数据库·mysql
weixin_4196583113 小时前
MySQL数据库备份与恢复
数据库·mysql