当前位置: 首页 > news >正文

【javaweb】学习日记Day7 - Mysql 数据库 DQL 多表设计

 之前学习过的SQL语句笔记总结戳这里→【数据库原理与应用 - 第六章】T-SQL 在SQL Server的使用_Roye_ack的博客-CSDN博客

目录

一、DQL 数据查询

1、基本查询

2、条件查询

3、分组查询

(1)聚合函数

① count函数

②  max min avg sum函数

(2)分组查询

① where和having的区别

(3)排序查询 

(4)分页查询

(5)案例

① if 和 case

二、多表设计

1、一对多 - 外键

 ① SQL 语句

② IDEA图形化操作

2、一对一  

3、多对多 

4、案例


一、DQL 数据查询

  

1、基本查询

*号代表查询所有字段,但不直观,影响效率(开发中尽量少用)

-- 查询name,entrydate
select name,entrydate from tb_emp;-- 查询所有字段
select * from tb_emp;-- 查询所有员工的name,entrydate,并起别名(姓名,入职日期)
select name as 姓名,entrydate as 入职日期 from tb_emp;-- 查询员工所在的职位类型(不重复)
select distinct job from tb_emp;

2、条件查询

like 占位符      (_匹配单个字符,%匹配任意字符)

-- 查询name为杨逍的员工
select * from tb_emp where name='杨逍';-- 查询id≤5的员工信息
select * from tb_emp where id<=5;-- 查询没有分配职位的员工信息
select * from tb_emp where job is null;-- 查询有职位的员工信息
select * from tb_emp where job is not null;-- 查询密码不等于123456的员工信息
select * from tb_emp where password<>'123456';-- 查询入职日期在[2000-01-01,2010-01-01]的员工信息
select * from tb_emp where entrydate >= '2000-01-01' and entrydate <= '2010-01-01';select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01';-- 查询入职日期在[2000-01-01,2010-01-01]且性别为女的员工信息
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01' and gender=2;-- 查询职位是2或3或4的员工信息
select * from tb_emp where job in (2,3,4);-- 查询姓名是两个字的员工信息
select * from tb_emp where name like '__';-- 查询姓张的员工信息
select * from tb_emp where name like '张%';

3、分组查询

(1)聚合函数

一列数据作为一个整体,进行纵向运算

不对null值进行统计

select 聚合函数(字段列表) from 表名
函数

功能

count统计数量
max最大值
min最小值
avg平均值
sum求和

① count函数

 推荐使用count(*) 

-- 1. 统计该企业员工数量
-- A.count(字段)
select count(id) from tb_emp;-- B.count(常量)
select count('A') from tb_emp;-- C.count(*)  -- 推荐!
select count(*) from tb_emp;

②  max min avg sum函数

-- 2. 统计该企业员工 ID 的平均值
select avg(id) from tb_emp;-- 3. 统计该企业最早入职的员工
select min(entrydate) from tb_emp;-- 4. 统计该企业最迟入职的员工
select max(entrydate) from tb_emp;-- 5. 统计该企业员工的 ID 之和
select sum(id) from tb_emp;

(2)分组查询

select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组过滤后的条件]

where > 聚合函数 > having,where不能对聚合函数进行判断,having可以

① where和having的区别

  • 执行时机不同:where是分组前过滤,不满足where,不参与分组;having是分组之后对结果进行过滤
  • 判断条件不同:having可以对聚合函数进行判断,where不行

 分组后,查询字段一般为聚合函数分组字段,查询其他字段无意义 

-- 分组
-- 1. 根据性别分组 , 统计男性和女性员工的数量
select gender,count(*) from tb_emp group by gender;-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*)>=2;

(3)排序查询 

select 字段列表 from 表名 [where 条件] [group by 分组字段名] order by 字段1 排序方式1,字段2 排序方式2...
  • ASC 升序(默认值)
  • DESC 降序
-- 1. 根据入职时间, 对员工进行升序排序
select * from tb_emp order by entrydate asc;-- 2. 根据入职时间, 对员工进行降序排序
select * from tb_emp order by entrydate desc;-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序
select * from tb_emp order by entrydate, update_time desc;

(4)分页查询

select 字段列表 from 表名 limit 起始索引,查询记录数;
  •  起始索引 = (页码 - 1)* 每页展示记录数
  • 如果查询的是第1页数据,则可以省略索引
  • 先查询再排序!
-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录
select * from tb_emp limit 0,5;-- 2. 查询 第1页 员工数据, 每页展示5条记录
select * from tb_emp limit 0,5;-- 3. 查询 第2页 员工数据, 每页展示5条记录
select * from tb_emp limit 5,5;-- 4. 查询 第3页 员工数据, 每页展示5条记录
select * from tb_emp limit 10,5;

(5)案例

-- 按需求完成员工管理的条件分页查询 - 根据输入条件,查询第一页数据,每一页展示10条记录
-- 输入条件:
-- 姓名:张
-- 性别:男
-- 入职时间:2000-01-01   2015-12-31
-- 要求查询结果根据最后修改时间进行倒序排序
-- 支持分页查询
select *
from tb_emp
where name like '张%'and gender = 1and entrydate between '2000-01-01' and '2015-12-31'
order by update_time desc
limit 10;

① if 和 case

  • if (条件表达式,true取值,false取值)
  • case 表达式 when 值1 then 结果1 when值2 then 结果2 ... else 结果n end
-- 按需求完成员工的性别统计  -- count(*)
-- if(条件表达式,true取值,false取值)
select if(gender = 1, '男', '女') as 性别, count(*) as 人数
from tb_emp
group by gender;-- 按需求完成员工的职位信息统计  -- count(*)
-- case 表达式 when 值1 then 结果1 when值2 then 结果2 ... else 结果n end
select (case jobwhen 1 then '班主任'when 2 then '讲师'when 3 then '学工主管'when 4 then '教研主管'else '未分配职位' end) 职位, count(*)人数
from tb_emp
group by job;

 

二、多表设计

1、一对多 - 外键

在作为【多】的表中将【一】的主键设置为外键

 ① SQL 语句

create table 表名(字段名 数据类型,...constraint 外键名称 foreign key (外键字段名) reference 主表(字段名)
);-- 指定完表后添加外键
alter table 表名 add constraint 外键名称 foreign key (外键字段名) reference 主表(字段名)

② IDEA图形化操作

  

  

     

2、一对一  

在任意一方加入外键,关联另一方的主键,并设置外键为唯一的unique

  

3、多对多 

建立一个中间表,该表包含两个外键,分别关联两方主键

  

4、案例

  

http://www.lryc.cn/news/141727.html

相关文章:

  • 线程的生命周期
  • GAN | 论文精读 Generative Adversarial Nets
  • Yolo系列-yolov2
  • Linux下的系统编程——vim/gcc编辑(二)
  • 2023年国赛 高教社杯数学建模思路 - 案例:最短时间生产计划安排
  • 芯科科技推出专为Amazon Sidewalk优化的全新片上系统和开发工具,加速Sidewalk网络采用
  • Kotlin 丰富的函数特性
  • Node.js怎么搭建HTTP服务器
  • 基于Redisson的联锁(MultiLock)
  • 人脸识别平台批量导入绑定设备的一种方法
  • MySQL—MySQL的NULL值是怎么存放的
  • sql server删除历史数据
  • 目标检测项目中,使用python+xml.etree.ElementTree修改xml格式标注文件中的类别名称
  • 最新域名和子域名信息收集技术
  • C语言基础之——指针(上)
  • 构建 NodeJS 影院预订微服务并使用 docker 部署(04/4)
  • SpringBootWeb案例 Part3
  • C++中using 用法
  • window下jdk安装及更换jdk版本的一些问题。
  • GPT4模型架构的泄漏与分析
  • GEE/PIE遥感大数据处理与典型案例丨数据整合Reduce、云端数据可视化、数据导入导出及资产管理、机器学习算法等
  • STM32--DMA
  • mongodb和redis的用途
  • 【动手学深度学习】--18.图像增广
  • 数据分析--统计学知识
  • matlab 计算点云协方差矩阵
  • python进阶之图像编程 pillow扩展库
  • TiCDC Canal-JSON 消息接收示例(Java 版)
  • SQLite、MySQL、PostgreSQL3个关系数据库之间的对比
  • 开源容灾备份软件,开源cdp备份软件