0
点赞
收藏
分享

微信扫一扫

数据库查询,order by,聚合函数,group by,limit 分页查询

程序员知识圈 2022-03-25 阅读 33
sql

数据准备

create database db2;
use db2;
create table product(
	pid int primary key auto_increment,
	pname varchar(10) not null,
	price double,
	category_id varchar(10)
);
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,'九阳电饭煲',5000,'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,'兰蔻面霜',300,'c003');
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);

select * from product;

在这里插入图片描述

select * from product where price between 200 and 1000;		-- 价格在 200-1000 之间的所有商品
select * from product where price >= 200 and price <=1000;

select * from product where price in(200,800);				-- 查看价格是 200 或 800 的所有商品
select * from product where price =200 or price =800;

select * from product where pname like '%裤%';				-- % 表示任意字符, 查询 名字里有裤字的 商品信息
select * from product where pname like '海%';				-- 以 海 字开头的 
select * from product where pname like '%_寇%';				-- 查询 第二个字为 寇 的商品, _ 是匹配单个字符

select * from product where category_id is null;			-- 查看 category_id 为 null的商品,  不要用 = 进行判断
select * from product where category_id is not null;		-- 查看 category_id 不为 null 的商品。
-- order by 一般放在 查询语句的最后面,Limit 语句除外
select * from product order by price desc;		-- 依据 price 进行降序排序
select * from product order by price desc,category_id desc;		-- 主依据是 price, 次要依据是 category_id

-- 聚合函数查询, 对某一列的值进行计算 
select count(pid) from product;						-- 查询 pid 这一列 的数据有多少行, 值为null的 不计入其中 
select count(*) from product;						-- 查询 这个表 有多少行数据
select count(pid) from product where price > 200;	-- 查询 价格大于 200 的有多少行数据

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

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

分组查询,先依据一个字段进行分组,分成几张临时表,然后对每张临时表 都分别进行 select。

-- 分组查询
select category_id,count(pid) from product group by category_id;		
-- 先依据 'category_id' 分出几张临时表,之后对每个临时表都分别进行 select

-- select后面 只能写分组依据的字段 和 聚合函数

-- 执行顺序: 先 from, 再 group by 分组,分成几张临时表 , 再 select , 再 having 进行筛选
select category_id,count(pid) 
from product 
group by category_id 
having count(pid) >3;

分页查询

select * from product limit 5;					--  显示前五条记录
select * from product limit 0,60;				--  从第 1 条记录开始, 显示 60条
select * from product limit 60,60;				--  从第 61 条记录开始, 显示 60条
-- 查询姓名 第二个字母不是 'A' 且 薪水 大于 1000的员工信息, 并按年薪 降序排列
-- 年薪: 12*月薪 + 奖金
-- ifnull(comm,0)  如果 comm 的值为 null, 则被当作 0 处理。

select * from emp where ename not like '_A%' and sal > 1000 order by (12*sal + ifnull(comm,0)) desc;

insert … select … 语句

-- 有一名学生学号是04005,其余信息与学号04003相同,请将学生信息插入表student。
insert into student 
select "04005",Sname,Ssex,Sage,Sdept from student where sno = "04003";

insert into 
	product2(pname,price) 
select 
	pname,price 
from 
	product;

sql语句的执行顺序以及流程

举报

相关推荐

0 条评论