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 结合 where
、order by
和 limit
例如,查找年龄最大且性别为“女”的前三名:
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