• 【MySQL】表的增删改查(基础)


    一、新增(Create)

    先创建一张表:

    create table student (id int,sn int comment '学号',name varchar(20),email varchar(20));
    
    • 1

    1.1 单行数据 + 全列插入

    插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致

    insert into student values (100,10000,'孙悟空','1111@qq.com');
    insert into student values (200,20000,'猪八戒','2222@qq.com');
    
    • 1
    • 2

    1.2 多行数据 + 指定列插入

    插入两条记录,value_list 数量必须和指定列数量及顺序一致

    insert into student (id,sn,name) values (300,30000,'沙悟能'),(400,40000,'唐僧');
    
    • 1

    最后得到的student
    在这里插入图片描述

    二、查询(Retrieve)

    创建考试成绩表:

     create table exam_result (
     id int,
     name varchar(20),
     chinese decimal(3,1),
     math decimal(3,1),
     english decimal(3,1)
     );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    插入测试数据:

    insert into exam_result (id,name,chinese,math,english) 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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.1 全列查询

    select * from exam_result;
    
    • 1

    在这里插入图片描述

    2.2 指定列查询

    select id,name,math from exam_result;
    
    • 1

    在这里插入图片描述

    2.3 查询字段为表达式

    表达式不包含字段:
    select id,name,10 from exam_result;
    表达式包含一个字段:
    select id,name,math+10 from exam_result;
    表达式包含多个字段:
    select id,name,chinese+math+english from exam_result;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.4 别名

    select id,name,chinese+math+english 总分 from exam_result;
    
    • 1

    在这里插入图片描述

    2.5 去重:distinct

    select distinct math from exam_result;
    
    • 1

    2.6 排序:order by

    -- ASC 为升序(从小到大)
    -- DESC 为降序(从大到小)
    -- 默认为 ASC
    select name,chinese from exam_result order by chinese;
    select name,chinese from exam_result order by chinese desc;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    在这里插入图片描述

    2.7 条件查询 where

    在这里插入图片描述
    在这里插入图片描述
    注意:
    🚕(1)where条件可以使用表达式,但不能使用别名。
    🚕(2)and的优先级高于or,在同时使用时,需要使用小括号()包裹优先执行的部分

    查询英语不及格的同学及英语成绩 ( < 60 )
    select english from exam_result where english < 60;
    
    • 1
    • 2
    查询语文成绩好于英语成绩的同学
    select chinese,english from exam_result where chinese > english;
    
    • 1
    • 2
    查询语文成绩大于80分,或英语成绩大于80分的同学
    select chinese,english from exam_result where chinese > 80 or english > 80;
    
    • 1
    • 2
    查询语文成绩在 [80, 90] 分的同学及语文成绩
    select chinese from exam_result where chinese between 80 and 90;
    
    • 1
    • 2
    查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
    select name,math from exam_result where math in (58,59,98,99);
    
    • 1
    • 2
    模糊查询like% 匹配任意多个(包括 0 个)字符:
    select name from exam_result where name like '孙%';
    _ 匹配严格的一个任意字符
    select name from exam_result where name like '孙_';
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.8 分页查询 limit

    按 id 进行分页,每页 3 条记录,分别显示 第 123 页:
    
    第一页:从 0 开始,筛选 3 条结果
    select id, name, math, english, chinese from exam_result order by id limit 3 offset 0;
    
    第二页:从 3 开始,筛选 3 条结果
    select id, name, math, english, chinese from exam_result order by id limit 3 offset 3;
    
    第三页:从 6 开始,筛选 3 条结果(结果不足不会有影响)
    select id, name, math, english, chinese from exam_result order by id limit 3 offset 6;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    三、修改(Update)

    将孙悟空同学的数学成绩变更为 80update exam_result set math=80 where name='孙悟空';
    
    将总成绩倒数前三的 3 位同学的英语成绩加上 30update exam_result set english = english + 30 order by chinese+math+english limit 3;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、删除(Delete)

    删除孙悟空同学的考试成绩:
    delete from exam_result where name = '孙悟空';
    
    删除整表数据:
    delete from exam_result;
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    石化人员定位方案:uBeacon+ibeacon融合定位特点
    人物故事 | 回顾美人建筑师,致世界建筑日
    人工神经网络是模拟什么进行信息处理的一种数学模型
    ZenCart 如何设置多个地区多个运费
    Java笔记 实用类(二)
    多图深度解析ArrayList源码
    docker--使用docker login 报错解决方案
    JWT -- Json Web token
    好的详细设计文档是可以降本增效的
    2023内蒙古工业大学计算机考研信息汇总
  • 原文地址:https://blog.csdn.net/m0_68101404/article/details/134430279