)
本文为数据库学习第三天的完整笔记涵盖DQL数据查询语言中的单表查询操作包括基本查询、条件查询比较、范围、逻辑、模糊、空值、排序查询、聚合函数、分页查询、分组查询以及DQL语句的编写流程与执行顺序适合零基础入门学习。一、DQL单表查询概述1.1 什么是DQLDQLData Query Language数据查询语言是SQL语言中最常用、最核心的部分主要用于对数据库表中的数据进行查询操作。1.2 单表查询单表查询是指只针对一张数据表进行数据查询的操作是后续复杂多表查询的基础。二、基本查询2.1 查询所有行所有列select * from product;2.2 查询指定列select pid,pname from product;2.3 去重查询使用distinct关键字去除重复数据-- 查看不重复的商品类别 select distinct category_id from product;三、条件查询where条件查询通过where子句对原始数据进行过滤筛选。注意where后面不能加聚合函数只能对原始列进行操作。3.1 比较查询使用,,,,,等比较运算符。-- 查询价格大于100的商品 select * from product where price 100; -- 查询价格不等于5000的商品 select * from product where price ! 5000; -- 或 select * from product where price 5000;3.2 范围查询between and连续范围between...and...是包含边界值的。-- (1) 查询商品价格在200到1000之间的所有商品信息 select * from product where price between 200 and 1000; -- (2) 另一种写法 select * from product where price 200 and price 1000;in()不连续范围-- (1) 查询商品编号为200,1000的所有商品信息 select * from product where price in (200,1000); -- (2) 另一种写法 select * from product where price 200 or price 1000;3.3 逻辑查询使用and,or,not等逻辑运算符。-- (1) 查询商品名称含有香字的所有商品信息 select * from product where pname like %香%; -- (2) 查询商品价格大于200并且小于1000的所有商品信息 select * from product where price 200 and price 1000; -- (3) 查询价格不是800的所有商品 select * from product where price ! 800; -- 或 select * from product where not (price800); -- (4) 查询价格不在200到800之间的所有商品信息 select * from product where not (price200 and price800); -- 或 select * from product where price200 or price800;3.4 模糊查询使用like关键字配合通配符进行模糊匹配。表格通配符说明%匹配任意字符0个或多个_匹配单个字符-- (1) 查询商品名称含有香字的所有商品信息 select * from product where pname like %香%; -- (2) 查询商品名称为三个字的商品信息 select * from product where pname like ___; -- (3) 查询商品名称以斯结尾并且是三个字的商品信息 select * from product where pname like __斯; -- (4) 查询以香开头并且是三个字的商品信息 select * from product where pname like 香__; -- (5) 查询以香开头的所有商品信息 select * from product where pname like 香%;3.5 空值查询使用is null和is not null判断空值。注意赋值的时候使用判断的时候使用is。-- (1) 查询商品分类id为空的商品信息 select * from product where category_id is null; -- (2) 查询商品分类不为空的商品信息 select * from product where category_id is not null;四、排序查询order by使用order by对查询结果进行排序。表格排序方式关键字说明升序asc默认可省略不写降序desc必须显式指定特点支持多列排序数值相同再按照第二列排以此类推。-- (1) 按价格进行升序排序查询所有的商品信息 select * from product order by price; -- (2) 按价格进行降序排序查询所有的商品信息 select * from product order by price desc; -- (3) 按照价格进行升序排序查询名称含有香的所有商品信息 select * from product where pname like %香% order by price; -- (4) 按照价格进行升序排序查询所有商品信息 select * from product order by price asc;五、聚合函数查询聚合函数用于对一组值进行计算返回单个值。表格函数作用说明min()最小值-max()最大值-avg()平均值-sum()求和-count()计数不计算空的列round()保留小数格式round(小数, 保留位数)-- (1) 查询商品的总条数 select count(*) from product; -- 或 select count(pid) from product; -- (2) 查询商品价格大于200的商品总条数 select count(*) from product where price 200; -- (3) 查询商品价格最大值 select max(price) from product; -- (4) 查询商品价格最小值 select min(price) from product; -- (5) 查询所有商品的平均价格 select avg(price) from product; -- (6) 查询所有商品的价格总和 select sum(price) from product; -- (7) 保留指定小数位保留2位小数 select round(avg(price),2) from product;注意聚合函数查询不能出现在where条件中。多个聚合函数可以同时使用select max(price), min(price), avg(price), sum(price), count(*) from product;六、分页查询limit使用limit实现分页展示数据。语法limit 索引值M, 查询个数N表格参数说明M起始索引计算方式(查询页-1) × 每页显示条数默认为0N每页显示条数-- (1) 从商品信息中开始查询前5条数据M默认为0可省略 select * from product limit 5; -- (2) 查询每页显示5条数据查询第2页数据 select * from product limit 5,5; -- (3) 查询商品信息中价格最低的前两件商品 -- 思路先排序后获取 select * from product order by price limit 2; -- (4) 每页显示5条数据要显示第3页的所有数据 select * from product limit 10,5;七、分组查询group by分组查询将数据按照指定列的值进行分组值相同的数据会被分到同一组。7.1 分组的原理分组之后相当于每个组存了一个表每个表里面存了对应组的数据。7.2 分组后不聚合分组后不配合聚合函数效果类似于去重-- (1) 按性别字段进行分组查询 select gender from person group by gender; -- 类似去重查询效果 select distinct gender from person;7.3 分组后聚合分组后配合聚合函数对每个组分别进行计算-- (1) 计算平均年龄 select avg(age) from person; -- (2) 计算人物个数 select count(*) from person; -- (3) 统计出不同性别的人平均年龄 select gender,avg(age) from person group by gender; -- (4) 统计出不同性别的人总个数 select gender,count(*) from person group by gender;7.4 having过滤having用于对分组后的结果进行过滤通常配合聚合函数使用。-- (1) 统计出不同性别的人的总个数 select gender,count(*) from person group by gender; -- (2) 统计出不同性别人数大于2的性别 select gender,count(*) from person group by gender having count(*) 2; -- (3) 统计年龄大于18的人按性别分组并且人数大于2的 select gender,count(*) from person where age 18 group by gender having count(*) 2;注意分组后必须聚合不聚合用处不大要不就是出错。八、DQL语句的编写流程select [distinct] | * | 列名 | 聚合函数() -- 负责展示 from 表名 -- 原始表 where 条件 -- 对原始表进行过滤 group by 分组的列... -- 值相同的分到一个组 having 条件 -- 对聚合函数结果进行过滤 order by 排序的列... -- 按照某一列的值进行排序 limit m,n; -- 分页展示九、DQL语句的执行顺序表格执行顺序关键字/操作作用1from确定数据的来源2where对原始表进行过滤3group by对原始表进行分组4聚合函数对分组后的数据进行计算5having对分组后的数据进行过滤6select确定要展示的数据7order by排序8limit m,n分页展示数据总结Day03主要学习了DQL单表查询的各种操作重点掌握基本查询select *、指定列、distinct去重条件查询比较运算符、范围查询between...and...、in()、逻辑运算符and、or、not、模糊查询like%、_、空值判断is null/is not null排序查询order by升序/降序支持多列排序聚合函数min()、max()、avg()、sum()、count()、round()注意不能出现在where中分页查询limit M,N的用法及页码计算公式(页码-1) × 每页条数分组查询group by配合聚合函数having对分组结果过滤DQL编写流程与执行顺序理解from → where → group by → 聚合 → having → select → order by → limit的执行逻辑下一篇将继续学习DQL多表查询敬请期待