• 【MYSQL】表的增删查改


    插入

    //指定列插入
    insert into table_name (c2,c3) values (v2,v3);
    
    //多行插入
    insert into table_name (c2,c3) values (v2,v3),(a2,a3)...;
    
    //全列插入
    insert into table_name values(v1,v2,...vn);
    
    其中,into是可以省略的
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    插入否则更新

    insert into table_name (c2,c3) values (v2,v3) on duplicate key update c2=v2,c3=v3;
    
    • 1

    0 row affected : 有冲突,但冲突和更新的值是一样的
    1 row affected : 没有冲突,直接插入了
    2 row affected : 有冲突,且冲突数据已经更新

    替换

    replace into table_name (sn,name) values(2001,'张飞');
    
    - 主键/唯一键 有冲突删除后插入
    - 主键/唯一键 没有冲突直接插入
    
    • 1
    • 2
    • 3
    • 4

    基本查询

    //全列查询
    select * from table_name;
    
    //按列查询
    select  c1,c2,c3 from table_name;
    
    //运算并命名
    seect name,chinese+math+english as [name] from table_name;
    
    as 可以带,也可以不带
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    WHERE 子句

    运算符

    运算符说明
    =等于,对NULL不安全,例如NULL=NULL 结果是 NULL
    <=>等于,对NULL安全,例如NULL <=> NULL 结果是 true
    !=,<>不等于
    BETWEEN a0 and b0范围匹配,[a0,b0]
    IN (option,…)如果是option的任意一个,返回true
    IS NULL是NULL
    IS NOT NULL不是NULL
    LIKE模糊匹配,%表示多个任意字符,_表示任意一个字符

    逻辑运算符:

    AND所有为true才为true
    OR有一个为true就是true
    NOT条件为true,结果是false

    where 子句案例

    英语不及格的同学及其英语成绩

    select name,english from score where english < 60;
    
    • 1

    语文成绩在[80,90]分的同学及语文成绩

    select name,chinese from score where chinese between 80 and 90;
    
    • 1

    数学成绩是58 或者59 或者 98 或者 99的同学和数学成绩

    select name,math form score where math in (58,59,98,99);
    
    • 1

    姓孙的同学 和 孙某同学

    select name from score where name like '孙%'; 
    
    select name from score where name like '孙_';
    
    • 1
    • 2
    • 3

    语文成绩好于英语成绩的同学

    select name,chinese,english from score where chinese > english;
    
    • 1

    总分在200分以下的

    select name,chinese+english+math as total from score where chinese+english+math < 200; 
    
    //执行顺序
    //1、先执行from
    //2、再按照筛选条件
    //3、显示筛选结果
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    语文成绩大于80 且 不姓孙的同学

    select name,chinese from score where chinese > 80 and name not like '孙%';
    
    • 1

    孙某同学,否则要求总成绩大于200 且语文成绩 小于数学成绩 并且英语成绩大于80

    select name,chinese,math,english,chinese+math+english as total from score where (chinese+math+english > 200 and chinese < math and english > 80) OR name like '孙%';
    
    • 1

    结果排序

    语法:

    select ... from table_name where ... ORDER BY [] [ASC|DESC]
    
    说明:
    ASC 为升序
    DESC为降序
    默认升序
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    排序样例

    同学和数学成绩,按数学成绩升序显示

    select name,math from score ORDER BY math ASC;
    
    • 1

    查询同学各科成绩,依次按照 数学降序、英语升序、语文升序显示

    select name,math,chinese,english from score ORDER BY math DESC, english ASC,chinese ASC; 
    
    • 1

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

    select name,math+chinese+english as total from score order by total;
    
    说明:先有数据才能排序,先进行筛选,再order by,可以使用total
    
    • 1
    • 2
    • 3

    查询性孙的同学或者性曹的同学数学成绩,结果降序显示

    select name,math from score where name = '孙%' or name = '曹%' order by math desc;
    
    • 1

    筛选分页结果

    语法:

    //从0开始,筛选n条结果
    select ... from table_name [where ...] [order by...] limit n;
    
    //从s开始,筛选n条结果
    select ... from table_name [where ...] [order by...] limit s, n;
    
    //从s开始,筛选n条结果
    select ... from table_name [where ...] [order by...] limit n offset s;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    update

    语法:

    查询到的结果进行列值更新,没有where条件,则进行全表更新。

    update table_name SET column1 = value1 [筛选条件];
    
    • 1

    将小明同学的数学成绩变更为 60 分,语文变更为70分

    update score set math=60 , chinese=70 where name = '小明';
    
    • 1

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

    //查:
    select name,chinese+math+english as total from score order by total desc limit 0,3; 
    
    //
    update score set math = math+30 order by chinese+math+english asc limit 3;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Delete

    语法:

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

    截断表

    TRUNCATE [TABLE] table_name;
    
    • 1

    两种方式都可以清空表,前者不会影响AUTO_INCREMENT值,后者会将其置为初始值。

    前者会包装成事物,而后者则直接运行,不会变成事物。

    去重表数据

    
    //创建一个结构一模一样的表
    create table noduplicate_table like duplicate_table;
    
    //查询去重
    select distinct * from duplicate_table;
    
    //插入查询到的数据
    insert into no_duplicate [select distinct * from duplicate_table];
    
    //
    rename .. to ..;  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    重命名的方式:等一切就绪后,统一放入,更新,生效等。

    聚合函数

    函数说明
    COUNT返回查询到数据的 数量
    SUM返回查询到的数据的总和 , 不是数字无意义
    AVG返回平均值,非数字无意义
    MAX返回最大值,非数字无意义
    MIN返回最小值,非数字无意义

    查询不同的数学成绩个数

    //先对math去重 , 而不是对统计出来的数据去重
    select COUNT(distinct math) as res from score;
    
    • 1
    • 2

    分组聚合统计

    分组目的:分组后聚合统计
    分组的本质即把一张大表拆分成多个组,进行各自的统计分组,也可以认为 “分表” ,在逻辑上拆成多个子表。

    select column1,column2, .. from table_name group by column3;
    
    • 1

    导入.sql文件

    source [path]
    
    • 1

    显示各个部门的最高薪资和最低薪资

    select deptno, max(sal) 最高 , avg(sal) 平均 from emp group by deptno;
    
    • 1

    每个部门的每种岗位的平均工资和最低工资

    select deptno ,job, avg(sal) 平均 , min(sal) 最低 from emp group by deptno , job order by deptno asc;
    
    • 1

    显示平均工资低于2000的部门和它的平均工资

    1、统计出来每一个部门的平均工资
    2、对聚合的结构进行判断

    select deptno,avg(sal) 平均工资 from emp group by deptno having avg < 2000; 
    
    • 1

    having:对聚合后的数据进行条件筛选。

    having 和 where

    where子句:对具体的任意列进行条件筛选

    having子句:对分组聚合之后的结果进行条件筛选

    执行顺序

    1、先确认从哪个表里拿数据
    2、where子句筛选条件
    3、分组
    4、按照分组后的数据进行聚合统计,重命名
    5、对分组聚合后的结果进行条件筛选(having)

    表的重新理解

    表不只是磁盘内的真实物理结构,中间筛选的,分出来的组,都可以理解成逻辑表。

  • 相关阅读:
    u盘上面 安装 ubuntu 系统
    使用 Java 操作 Redis
    S4HANA - Cost Elements成本要素
    【服务器数据恢复】Dell服务器raid5磁盘阵列多块硬盘离线的数据恢复案例
    算法:包含min函数的栈
    2195. 深海机器人问题(网络流,费用流,上下界可行流,网格图模型)
    Router-view
    vue防抖和限流
    MySQL | SELECT
    说一下 toRef、toRefs,以及他们的区别
  • 原文地址:https://blog.csdn.net/m0_73209194/article/details/133212938