MySql从0-1学习-第二天DML和DQL学习

DML

对数据库表中的数据进行增删改操作

Insert 语法

复制代码
//指定字段添加数据
insert into 表名(字段名1,字段名2) values (值1,值2);

//全部字段添加数据:
insert into 表名 values(值1,值2...);

//批量添加数据(指定字段)
insert into 表名(字段名1,字段名2) values(值1,值2),(值1,值2);

//批量添加数据(全部字段)
insert into 表名 values(值1,值2),(值1,值2);

update 语法

复制代码
//修改数据,如果没有where 语句 会更新整张表的数据
update 表名 set 字段1=值1,字段2=值2... where 语句;

delete 语法

复制代码
//删除语句,如果没有where语句,会删除整张表的数据
delete  from 表名 where语句;

DQL

对数据库中的数据进行查询

查询语法

  • 基本查询

  • 条件查询

  • 分组查询

  • 排序查询

  • 分页查询

    select
    字段列表
    from
    表名列表
    where
    条件列表
    group by
    分组字段列表
    having
    分组后条件列表
    limit
    分页参数

基本查询

复制代码
//查询多个字段
select 字段1,字段2 from 表名;

//查询所有的字段
select * from 表名;

//查询多个字段,并设置别名(as 关键字可省略)
select 字段1 as 别名1, 字段2 as 别名2 from 表名;

//去除重复记录
select distinct 字段列表 from 表名;

条件查询

复制代码
select 字段列表 from 表名 where 条件列表;
比较运算符 功能
> 大于
>= 大于等于
< 小于
<= 小于等于
= 等于
<>或!= 不等于
between...and... 在某个范围之内(含最小/最大值)
in(...) 在in之后的列表中的值,多选一
like 占位符 模糊匹配(_匹配单个字符,%匹配多个字符)
is null 是null
逻辑运算符 功能
and或&& 并且(多个条件同时成立)
or 或
not 或! 非.不是
查询示例
复制代码
//查询名为张三的用户,从tb_user表中
select * from tb_user where name='张三';

//查询id小于等于5的用户,从tb_user表中
select * from tb_user where id<=5;

//查询没有分配职位的用户,从tb_user表中
select * from tb_user where job is null;

//查询有职位的用户,从tb_user表中
select * from tb_user where job is not null;

//查询用户密码不是'123456'的用户,从tb_user表中
select * from tb_user where password !='123456';
//效果同上
select * from tb_user where password <>'123456';

//查询用户入职日期从2021-01-02到2023-01-01的用户,从tb_user表中
select * from tb_user where join_time >=2021-01-01 and join_time <=2023-01-01;
//效果同上
select * from tb_user where join_time between 2021-01-01 and 2023-01-01;

//查询用户入职日期从2021-01-02到2023-01-01的用户,并且性别为女的,从tb_user表中
select * from tb_user where join_time >=2021-01-01 and join_time <=2023-01-01 and gender=1;
//效果同上
select * from tb_user where join_time between 2021-01-01 and 2023-01-01 and gender=1;
//效果同上
select * from tb_user where join_time between 2021-01-01 and 2023-01-01 && gender=1;

//查询职位是2,3,4的用户,从tb_user表中
select * from tb_user where job=2 or job =3 or job =4;
//效果同上
select * from tb_user where job in(2,3,4);

//查询姓名为两个字的用户,从tb_user表中
select * from tb_user where name like '__';

//查询姓名姓张的用户,从tb_user表中
select * from tb_user where name like '张%';

分组查询

学习分组查询前,需要先了解聚合函数

聚合函数
  • 介绍: 将一列数据作为一个整体,进行纵向计算
  • 语法: select 聚合函数(字段列表) from 表名;
函数 功能
count 统计数量
max 最大值
min 最小值
avg 求平均值
sum 求和
示例
复制代码
//统计用户数量,从tb_user表中(不对null值字段进行统计)
select count(字段1)  from tb_user;
//效果同上
select count(*)  from tb_user;

//统计入职最早的用户,从tb_user表中
select min(join_time) from tb_user;

//统计入职最晚的用户,从tb_user表中
select max(join_time) from tb_user;

//统计用户id的平均值,从tb_user表中
select avg(id) from tb_user;

//统计用户id的和,从tb_user表中
select sum(id) from tb_user;
分组查询
复制代码
//分组查询语法([ ]代表是可选项)
select 字段列表 from 表名  [where 条件] group by 分组字段名 [having 分组后过滤条件];

//根据性别分组,分别统计各自的数量,从tb_user表中
//(分组查询的返回字段有两类,一个是分组字段,一个是聚合字段)
select gender,count(*) from tb_user group by  gender;

//先查询入职日期在'2021-01-01'(包含)以前的用户,并对结果进行职位分组,获取员工数量大于等于2的职位
select job,count(*) from tb_user where join_time <= '2021-01-01' group by job having count(*) >=2;

排序查询

排序方式有两种

  • DESC 降序

  • ASC 升序(默认值)

    select 字段列表 from 表名 [where 条件] [group by 分组字段] order by 字段1 排序方式1, 字段2 排序方式2

示例代码

复制代码
//对入职时间升序排序查询(asc可省略)
select * from tb_user order by join_time asc;

//对入职时间降序排序查询
select * from tb_user order by join_time desc;

//对入职时间升序排序查询(asc可省略),如果时间相同的,根据更新时间降序排序

select * from tb_user order by join_time asc, update_time desc;

分页查询

复制代码
select 字段列表  from 表名 limit 起始索引,查询记录数;
示例代码
复制代码
//从起始数据0开始查询,每页展示五条记录
select * from tb_user limit 0,5;

//查询第一页的数据,每页展示五条记录
select * from tb_user limit 0,5;

//查询第二页的数据,每页展示五条记录
select * from tb_user limit 1*5,5;

//查询第三页的数据,每页展示五条记录
select * from tb_user limit 2*5,5;

//根据姓名,性别,入职时间范围,分页查询
select * from tb_user where name like '%张%' and gender='男' and join_time between '2021-01-01' and '2023-01-01' limit 0,5;
相关推荐
千桐科技10 小时前
数据库设计最佳实践:我们团队沉淀下来的规范
数据库·代码规范·设计
zhangfeng113310 小时前
学习文本大模型的学习路径,各种大模型对比和分类以及各个大模型对硬件的要求,开源大模型有哪些
学习·分类·开源
青衫码上行10 小时前
【Java Web学习 | 第九篇】JavaScript(3) 数组+函数
java·开发语言·前端·javascript·学习
踏浪无痕10 小时前
PostgreSQL实例进程:从启动到运行的完整故事
数据库·postgresql
q***188411 小时前
redis的下载和安装详解
数据库·redis·缓存
腾讯云数据库11 小时前
「腾讯云NoSQL」技术之向量数据库篇:腾讯云向量数据库如何实现召回不变,成本减半?
数据库·nosql·腾讯云·向量数据库·腾讯云nosql
yaso_zhang11 小时前
jetson开机之前自启脚本sudo ifconfig 如何不需要输入密码
数据库·postgresql
aoxiang_ywj11 小时前
SQLite 速成学习
数据库·sqlite
普普通通的南瓜11 小时前
网站提示 “不安全”?免费 SSL 证书一键解决
网络·数据库·网络协议·算法·安全·iphone·ssl
张3蜂11 小时前
import org.junit.Test; 是什么
数据库·junit