本节讲解排序查询、聚合查询、分组查询、分页查询,以及INSERT INTO SELECT语句、SELECT INTO FROM语句。

排序查询(order by)

可以使用 MySQL 的 order by 字句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。

语法:

select 
 字段名1,字段名2,……
from 表名
order by 字段名1 [asc|desc],字段名2[asc|desc]……

特点:

1.asc代表升序,desc代表降序,如果不写默认升序

2.order by用于子句中可以支持单个字段,多个字段,表达式,函数,别名

3.order by子句,放在查询语句的最后面。LIMIT子句除外

4.多个字段排序就是先按字段1排序,排完后相同的部分再按字段2排序,有个优先级。

示例:

还是使用上一节的数据,可以在Navicat中把product删除,重新创建。

-- 排序查询
-- 1.使用价格排序(降序)
select * from product order by price desc;
-- 2.在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id desc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;

后面会附上本节的所有代码。

集合排序(count、 sum、max、min、avg)

之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值;另外聚合函数会忽略空值。

聚合函数作用
count()统计指定列不为NULL的记录行数;
sum()计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0
max()计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;
min()计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;
avg()计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0

示例:

-- 1 查询商品的总条数
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(*) from product where price > 200;
-- 3 查询分类为'c001'的所有商品的总和
select sum(price) from product where category_id = 'c001';
-- 4 查询商品的最大价格
select max(price) from product;
-- 5 查询商品的最小价格
select min(price) from product;
-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id = 'c002';

后面会附上本节的所有代码。

聚合查询对NULL的处理:

1、count函数对null值的处理

如果count函数的参数为星号(*),则统计所有记录的个数。而如果参数为某字段,不统计含null值的记录个数。

为*时,只要一行不全是NULL,就算一个。

2、sum和avg函数对null值的处理

这两个函数忽略null值的存在,就好象该条记录不存在一样。

3、max和min函数对null值的处理

max和min两个函数同样忽略null值的存在。

要是不想被忽略,可以将空设为0

分组查询(group by)

语法:

select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;

示例:

-- 1 统计各个分类商品的个数
select category_id ,count(*) from product group by category_id ;

分组之后的筛选 having

  • 分组之后对统计结果进行筛选的话必须使用having,不能使用where
  • where子句用来筛选 FROM 子句中指定的操作所产生的行
  • group by 子句用来分组 WHERE 子句的输出。
  • having 子句用来从分组的结果中筛选行

语法:

select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;

示例:

-- 2.统计各个分类商品的个数,且只显示个数大于4的信息
select category_id ,count(*) from product group by category_id having count(*) > 1;

分页查询(limit)

分页查询在项目开发中常见,由于数据量很大,显示屏长度有限,因此对数据需要采取分页显示方式。例如数据共有30条,每页显示5条,第一页显示1-5条,第二页显示6-10条。

分页效果示例

语法:

方式1:显示前n条

select 字段1,字段2... from 表明 limit n

方式2:分页显示

select 字段1,字段2... from 表明 limit m,n

m: 整数,表几条索引开始,计算方式 (当前页-1)*每页显示条数

n: 整数,表查询多少条数据

注意:第一条的索引值为0

INSERT INTO SELECT语句

将一张表的数据导入到另一张表中,可以使用INSERT INTO SELECT语句 。

格式一:注意Table1的value与Table2的field一一对应

insert into Table2(field1,field2,…) select value1,value2,… from Table1 

格式二:直接将Table1导入Table2

insert into Table2 select * from Table1

注意:要求目标表Table2必须存在

SELECT INTO FROM语句

语法:

SELECT vale1, value2 into Table2 from Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

代码

use mydb1;
-- 创建商品表:
create table product(
 pid int primary key auto_increment, -- 商品编号
 pname varchar(20) not null , -- 商品名字
 price double,  -- 商品价格
 category_id varchar(20) -- 商品所属分类
);
-- 添加数据
insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',200,'c001' );

insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
 
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

-- 排序查询
-- 1.使用价格排序(降序)
select * from product order by price desc;

-- 2.在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id desc;

-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;


-- 聚合查询
-- 1 查询商品的总条数
    -- 非空的(多个列的话只要不全为空就不算空)
select count(*) from product;

-- 2 查询价格大于200商品的总条数
select count(*) from product where price > 200;

-- 3 查询分类为'c001'的所有商品的价格总和
select sum(price) from product where category_id = 'c001';

-- 4 查询商品的最大价格
select max(price) from product;

-- 5 查询商品的最小价格
select min(price) from product;

-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id = 'c002';


-- 分组查询
-- 1 统计各个分类商品的个数
  -- 先对整体的表进行切割(根据依据category_id,然后select)
  -- 要是group by多个字段,则需要这几个字段的值同时相同才会被划分为一类
    -- 分组时候,select的后面只能写分组字段和聚合函数
select category_id,count(pid) from product GROUP BY category_id;
  
    -- 分组之后的筛选
    -- 统计各个分类商品的个数,且只显示个数大于4的信息
    -- 执行顺序:from一个表,ground by对整表进行切割,count统计,select筛选,having进行筛选,order by排序
select category_id ,count(*) from product group by category_id having count(*) >= 4 order by;


-- 分页查询
-- 查询product表的前5条记录 
select * from product limit 5 ;

-- 从第4条开始显示,显示5条 
select * from product limit 3,5 ;

-- 分页显示的原理
 -- 假设一页60条
# select * from product limit 0,60 ;           -- 第一页
# select * from product limit 60,60;           -- 第二页
# select * from product limit 120,60 ;         -- 第三页
# select * from product limit (n-1)*60,60;     -- 第n页


-- insert into Table2(field1,field2,…) select value1,value2,… from Table1 
-- 或者:
-- insert into Table2 select * from Table1
select * from product;
-- 创建Table2
create table product2(
    pname VARCHAR(20),
    price DOUBLE
);
insert into product2(pname,price) SELECT pname,price from product ;
 
create table product3(
 category_id varchar(20),
 product_count int
);
INSERT into product3 select category_id,count(*) from product GROUP BY category_id;

基本操作总结

3-MySQL基本查询-DQL


声明:内容来源于B站视频《2022黑马程序员最新MySQL知识精讲+mysql实战案例_零基础mysql数据库入门到高级全套教程》,博客内容仅作学习参考使用。


本文标签: 数据库MySQL

版权声明 ▶ 本网站名称:陶小桃Blog
▶ 本文链接:https://www.52txr.cn/2022/mysql11.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处并附带原文链接!!
▶ 站长邮箱 [email protected][email protected] ,如不方便留言可邮件联系。

小陶的个人微信公众号

学累了就来张美女照片养养眼吧,身体是革命的本钱,要多多休息哦~ 随机美女图片

最后修改:2022 年 06 月 05 日
如果觉得我的文章对你有用,请随意赞赏!