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

MySQL-单表查询

目录

1. 基础查询

1.1 查询所有字段

1.2 查询指定字段

2. 条件查询(WHERE 子句)

2.1 比较运算符

2.2 范围查询

2.3 指定集合查询

 2.4 模糊查询

 2.5 判空查询

 3. 分页查询(limit 子句)

3.1 获取前 n 条记录

3.2 指定起始索引和查询条数

 3.3 按页查询

4. 数据排序(order by 子句)

4.1 按年龄升序排列

4.2 按年龄降序排列

4.3 结合 where、order by 和 limit

 5. 数据统计(聚合函数)

5.1 计算年龄总和

5.2 计算学生总数

6. 分组查询(group by 子句)

6.1 计算每个班级的总年龄

 6.2 计算每个班级的最大年龄

 6.3 计算每个班级的学生人数

 结论


        在日常数据库操作中,我们经常需要对数据进行查询、筛选、排序和统计分析。本文将介绍 mysql 查询语句的基本用法,包括 select 语句、where 条件筛选、limit 分页、order by 排序以及 group by 分组聚合等。

假设我们有一张student表

1. 基础查询

1.1 查询所有字段

select * from student;

1.2 查询指定字段

如果只想获取特定列的数据,可以在 SELECT 语句中指定列名:

select name, sex, age from student;

2. 条件查询(WHERE 子句)

2.1 比较运算符

where 子句可以用于筛选符合条件的数据,支持以下运算符:

  • = :等于
  • <>!= :不等于
  • >>= :大于、大于等于
  • <<= :小于、小于等于
select * from student where name = '张三';
select * from student where age <> 20;
select * from student where age <= 20 and age > 10;

2.2 范围查询

between ... and ... 用于查询某个范围内的数据:

select * from student where id between 2 and 24;

2.3 指定集合查询

in 用于匹配多个具体的值:

select * from student where id in (1, 4, 11, 14);

not in 代表不在指定集合中的数据:

select * from student where id not in (1, 4, 11, 14);

 2.4 模糊查询

like 关键字用于模糊匹配:

  • % 表示任意数量的字符
  • _ 表示一个字符
select * from student where name like '张%';  -- 以“张”开头的所有数据
select * from student where name like '张__'; -- 以“张”开头且后面跟两个字符的数据

 2.5 判空查询

is null 用于查找值为空的数据:

select * from student where sex is null;

is not null 用于查找不为空的数据:

select * from student where sex is not null;

 3. 分页查询(limit 子句)

当数据量较大时,我们通常需要分页查询,每页显示固定数量的数据。

3.1 获取前 n 条记录

select * from student limit 5;  -- 取前5条数据

3.2 指定起始索引和查询条数

select * from student limit 3, 5; -- 跳过前三条数据,获取接下来的5条

 3.3 按页查询

假设每页显示 4 条数据:

-- 第一页(索引 0 开始)
select * from student limit 0, 4;

-- 第二页
select * from student limit 4, 4;

-- 第三页
select * from student limit 8, 4;

 可以使用 (page-1) * pagesize 计算 limit 的偏移量:

select * from student limit (page-1) * pagesize, pagesize;

4. 数据排序(order by 子句)

        默认情况下,数据库返回的查询结果顺序可能并不是我们想要的,我们可以使用 order by 进行排序。

4.1 按年龄升序排列

select * from student order by age;

4.2 按年龄降序排列

select * from student order by age desc;

4.3 结合 whereorder bylimit

例如,查找年龄最大且性别为“女”的前三名:

select * from student where sex='女' order by age desc limit 3;

 5. 数据统计(聚合函数)

mysql 提供了一些常用的聚合函数,用于数据的统计分析。

5.1 计算年龄总和

select sum(age) from student;

5.2 计算学生总数

select count(*) from student;

 count(*) 计算所有记录,count(列名) 只计算非空记录。

6. 分组查询(group by 子句)

当我们需要按照某个字段进行分组统计时,可以使用 group by 进行分组查询。

6.1 计算每个班级的总年龄

select classnum, sum(age) from student group by classnum;

 6.2 计算每个班级的最大年龄

select classnum, max(age) from student group by classnum;

 6.3 计算每个班级的学生人数

select classnum, count(*) from student group by classnum;

 结论

本文介绍了 mysql 查询的基本用法,包括:

  • select 查询所有或特定字段
  • where 条件查询(比较、范围、集合、模糊匹配、空值判断)
  • limit 分页查询
  • order by 排序查询
  • group by 分组统计查询

这些 sql 语法是数据库查询的基础,好长时间前学的了......一直没整理xixixixixixi

-- 查找
select * from student
select name,sex,age from student


-- where子语句实现条件查找
-- 运算符:= 、 >= 、 <= 、> 、<、 <>(!=) 不等于
select * from student where name = '111'
select * from student where age <>20
-- 逻辑运算符 :and 、or、not 非,主要用在is in
select * from student where age <=20 and age>10
-- between .. and .. 介于两个值之间
select * from student where id between 2 and 24 
-- in 包含   not in不包含
select * from student where id in(1,4,11,14)
select * from student where id not in(1,4,11,14)
-- 模糊查找 like  配合占位符一起使用  :_表示一位字符  %任意位字符
select * from student where name like "张%"
select * from student where name like "张__"
-- is null 判空   is not null 不是空
select * from student where sex is null


-- limit子语句,分页查找 限制查询的个数 
-- limit 起始索引值,要查询的个数      limit 要查询的个数 offset 起始索引值
select * from student limit 3,5
    -- 查找性别为女的前三条数据
select * from student where sex = "女" limit 0,3 
-- 一页四条数据 实现分页
-- 第一页
select * from student limit 0,4
-- 第二页
select * from student limit 4,4
-- 第三页
select * from student limit 8,4
-- ...... 找规律
select * from student limit (page-1)*pagesize,pagesize


-- order子语句 实现排序
-- order by 列名 desc降序/asc升序(默认升序)
select * from student order by age

-- 以上三个子语句结合使用时的顺序为 where   order by     limit 先筛选再排序最后分页
select * from student  where sex="女" order by age desc limit 0,3

-- 聚合函数 
-- sum() 求和   avg()求平均  max()求最大值  min() 最小值  count()求记录数量
select sum(age) from student
select count(*) from student -- 不统计为空的
-- 分组函数  group by 字段名称 :把一个classnum的视为一组
select sum(age) from student group by classnum
select max(age),classnum from student group by classnum

相关文章:

  • Fisher 信息矩阵公式原理:使用似然估计,二阶导数等知识点
  • 神经网络微调技术解析
  • 基于Qlearning强化学习的钟摆直立平衡控制策略matlab仿真
  • 大模型 VS 传统算法:人工智能时代的“新老对话“
  • maven在idea上搭建
  • C语言经典代码练习题
  • 【Linux我做主】浅谈Shell及其原理
  • JAVA中关于图形化界面的学习(GUI)动作监听,鼠标监听,键盘监听
  • ESP32的IDF开发学习-驱动ov2640并显示在屏幕上
  • C++(八)vector
  • 国产编辑器EverEdit - 语法着色文件的语法
  • 【一起来学kubernetes】16、CronJob使用详解
  • OpenGL 将屏幕上的二维坐标转换为三维空间中的一个点
  • macOS homebrew - 切换源
  • 当前有哪些学习资料可以帮助我学习整机性能方面的知识吗
  • Android中的layout_gravity与gravity属性
  • Canary Capital 向 SEC 递交首个 SUI ETF 申请文件
  • 如何用AI轻松制作PPT,提升工作效率和演讲质量
  • 【MySQL】函数
  • Wi-Fi NAN 架构(Wi-Fi Aware Specification v4.0,第二章:2.1~2.2)
  • 深圳宝安区一宗涉宅用地中止出让,起始总价86.27亿元
  • “中国游”带火“中国购”,“即买即退”让外国游客购物更丝滑
  • 在上海生活8年,13岁英国女孩把城市记忆写进歌里
  • 中公教育:去年全面扭亏,经营性现金流增长169.6%
  • 特朗普将举行集会庆祝重返白宫执政百日,被指时机不当
  • 文化体验+商业消费+服务创新,上海搭建入境旅游新模式