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

mySql数据库学习002-表数据查询操作

表数据查询操作

表数据如下:

idnameagegenderclasscreatedAtupdatedAt
1张三20一班2024-04-08 09:15:092024-04-08 09:15:09
2李四19一班2024-04-08 09:15:092024-04-08 09:15:09
3王五21二班2024-04-08 09:15:092024-04-08 09:15:09
4赵六18二班2024-04-08 09:15:092024-04-08 09:15:09
5孙七19三班2024-04-08 09:15:092024-04-08 09:15:09
6周八19三班2024-04-08 09:15:092024-04-08 09:15:09
7张三丰152024-04-08 09:15:092024-04-08 09:15:09
一、基础查询
select id, name from student

查询结果

idname
1张三
2李四
3王五
4赵六
5孙七
6周八
7张三丰

注意

  • 关键词 selectid,name为要查询的字段,使用 * 表示查询所有字段;

二、where条件查询
select * from student where gender = '男'

查询结果

idnameagegenderclasscreatedAtupdatedAt
1张三20一班2024-04-08 09:15:092024-04-08 09:15:09
5孙七19三班2024-04-08 09:15:092024-04-08 09:15:09
6周八19三班2024-04-08 09:15:092024-04-08 09:15:09
7张三丰152024-04-08 09:15:092024-04-08 09:15:09

解释

  • where 后表示查询条件;
  • 另外,where 语句支持 >>=<<==!=andornotbetween ... and ...is nullis not nullinlike
select * from student where gender = '女' and age > 20
select * from student where gender = '女' or age < 19
select * from student where age > 15 and age < 20
select * from student where age between 16 and 19
select * from student where class is null
select * from student where class is not null
select * from student where age in(18,19)
select * from student where name like '李%'
select * from student where name like '%六'
select * from student where name like '%三%'

三、排序
select * from student where gender = '男' order by age desc

查询结果

idnameagegenderclasscreatedAtupdatedAt
1张三20一班2024-04-08 09:15:092024-04-08 09:15:09
5孙七19三班2024-04-08 09:15:092024-04-08 09:15:09
6周八19三班2024-04-08 09:15:092024-04-08 09:15:09
7张三丰152024-04-08 09:15:092024-04-08 09:15:09

解释:

  • order by 为排序的关键词,其中 age 为排序的字段名,desc 表示倒序排列,asc表示正序排列;

四、分页查询
select * from student limit 3 offset 0
select * from student limit 0, 3

查询结果

idnameagegenderclasscreatedAtupdatedAt
1张三20一班2024-04-08 09:15:092024-04-08 09:15:09
2李四19一班2024-04-08 09:15:092024-04-08 09:15:09
3王五21二班2024-04-08 09:15:092024-04-08 09:15:09

解释:

  • limit 表示分页大小,offset 表示偏移量;
  • 在不使用 offset 关键词时,limit 后第一个参数为偏移量,第二个参数为分页大小;
  • 偏移量 通常需要根据前端传值的pageSize, pageNum计算得来;

五、聚合函数
select sum(age) sum_age from student 
select avg(age) avg_age from student
select count(*) stu_count from student
select max(age) max_age from student
select min(age) min_age from student

查询结果

sum_age
131

解释:

  • 聚合函数需要传参,参数可以为 *,或者数据表中的字段;
  • 另外,对于聚合函数的结果,可以设置 别名,如 sum_age 表示查询到数据的年龄和;

六、分组查询
select class, count(class) class_count from student group by class order by class_count desc

查询结果:

classclass_count
三班2
一班2
二班2
Null0

解释:

  • 其中 class 为分组字段,group by 为分组查询关键词,class_count为别名;
  • 在对数据进行分组的时候,select 后面必须是分组字段或者聚合函数;

七、having条件查询
select class, avg(age) avgAge from student group by class having avgAge > 19

查询结果:

classavgAge
一班19.5
二班19.5

解释:

  • class 为分组字段,avg(age) 聚合函数求年龄平均值,group by 分组关键词,having 分组关键词,表示从返回的结果集中筛选平均年龄大于等于19的班级;
  • 注意:最好设置聚合函数结果的别名avgAge

八、Q & A
  1. 什么是结果集?有什么特点?与表有什么区别与联系?
  • 通过查询语句从数据表中查询出来的结果成为结果集;以表的形式呈现;结果集和查询的表不是同一张表,结果集来自数据表;结果集保存在内存中,而数据表保存在硬盘上;
  1. 什么是聚合函数?怎么使用聚合函数?
  • 对表中的数据进行统计和计算,一般结合分组(GROUP BY)来使用,用于统计和计算分组数据
  • COUNT() 计算查询数据的数据总量
  • SUM() 计算查询结果中所有指定字段的和
  • AVG() 计算查询结果中所有指定字段的平均值
  • MAX() 求查询结果中指定字段的最大值
  • MIN() 求查询结果中指定字段的最小值;
  1. having 与 where 的区别是什么?
  • where 是去数据表中查询符合条件的数据,返回结果集;
  • having 是去数据集中查询符合条件的数据,可以对分组之后查询到的结果进行筛选;

相关文章:

  • three.js尝试渲染gbl模型成功!(三)
  • unable to find a medium containing a live file system解决办法!
  • c++的学习之路:22、多态(1)
  • 【MATLAB源码-第184期】基于matlab的FNN预测人民币美元汇率 输出预测图误差图RMSE R2 MAE MBE等指标
  • [lesson17]对象的构造(上)
  • 设计模式:适配器模式
  • Linux 内核的构建块:深入探索 C 结构体的应用
  • 蓝桥杯-冶炼金属(二分求最大最小)
  • 搭建前后端的链接(java)
  • 5.4Python之可变类型与列表的深浅拷贝
  • React 状态管理:安全高效地修改对象属性的 3 种方法
  • Linux CPU利用率
  • 通往 AGI 的道路上,OpenAI 逐渐构建了全模态的工具集
  • 【算法优选】 动态规划之简单多状态dp问题——贰
  • OpenHarmony分布式软总线API调用测试工具 softbus_tool使用说明
  • [笔记] php常见简单功能及函数
  • 《深入 React 技术栈》
  • 5、React组件事件详解
  • CEF与代理
  • java 多线程基础, 我觉得还是有必要看看的
  • java2019面试题北京
  • JavaScript设计模式与开发实践系列之策略模式
  • k8s如何管理Pod
  • Koa2 之文件上传下载
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • python 装饰器(一)
  • Spring Cloud Feign的两种使用姿势
  • 从0实现一个tiny react(三)生命周期
  • 基于 Babel 的 npm 包最小化设置
  • 简单基于spring的redis配置(单机和集群模式)
  • 开源地图数据可视化库——mapnik
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 自动记录MySQL慢查询快照脚本
  • 《天龙八部3D》Unity技术方案揭秘
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #1015 : KMP算法
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #Ubuntu(修改root信息)
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)VC++中ondraw在什么时候调用的
  • .Net 8.0 新的变化
  • .NET Framework杂记
  • .NET 药厂业务系统 CPU爆高分析
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .net通用权限框架B/S (三)--MODEL层(2)
  • .pop ----remove 删除
  • @拔赤:Web前端开发十日谈
  • [ C++ ] STL_list 使用及其模拟实现
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码