-- 1、poduction产品表
create table poduction(
pid int not null auto_increment,-- id
pno int null,-- 产品编号
pname varchar(50) null,-- 产品名称
pdate date null,-- 生产日期
primary key(pid)-- 设置pid列主键自增
);
commit;
select * from poduction;
insert into poduction(pno,pname,pdate) values(1,'产品A','2018-11-12'),
(2,'产品B','2017-05-25'),
(3,'产品C','2016-10-06');
commit;
#--------------------------------------------------------------------------------#
-- 2、sales销售人员表
create table sales(
sid int not null auto_increment,
sno int null,-- 销售人员编号
sname varchar(50) null,-- 销售人员姓名
ano int null,-- 代理商编号
primary key(sid)-- 设置sid主键自增
);
commit;
insert into sales(sno,sname,ano) values(1,'小赵',2),
(2,'小钱',2),
(3,'小孙',3),
(4,'小李',1),
(5,'小石',3),
(6,'小刘',1);
commit;
select * from sales;
#--------------------------------------------------------------------------------#
-- 3、agent代理商表
create table agent(
aid int not null auto_increment,
ano int null,-- 代理商编号
aname varchar(100),-- 代理商名称
primary key(aid)-- 设置aid主键自增
);
commit;
insert into agent(ano,aname) values(1,'AAA公司'),
(2,'BBB公司'),
(3,'CCC公司');
commit;
select * from agent;
#---------------------------------------------------------------------------#
-- 4、deal销售量表
create table deal(
did int not null auto_increment,
sno int null,-- 销售人员编号
pno int null,-- 产品编号
volume int null,-- 销售量
primary key(did)
);
commit;
insert into deal(sno,pno,volume) values(1,1,21),
(1,2,56),
(1,3,33),
(2,2,120),
(2,1,32),
(2,3,26),
(3,1,73),
(3,3,45),
(4,2,111),
(4,3,84),
(5,3,52),
(5,1,38),
(5,2,91),
(6,2,36),
(6,3,48),
(6,1,15);
commit;
-- 查询表
select * from poduction;
select * from sales;
select * from agent;
select * from deal;
-- 删除这个表,彻底删除
drop table dee;
#---------------------------------------------------------------#
八个题目如下:总答案,和答案分步
1.查询总销量排在前两名的产品(使用in关键字)?
-- 在多表连接中,经常会取临时表,取别名,例如下面的t2,d2都是别名
-- 最终的答案如下:
select t2.pno,t2.pname,d2.sums from (
select * from poduction where pno in(
select t1.pno2 from (
select pno as pno2,sum(volume) as sum2 from deal group by pno order by sum2 desc limit 2) t1
))t2
inner join
(select d.pno,sum(d.volume) as sums
from deal d
group by d.pno)d2
on t2.pno=d2.pno
-- t1:括号内是,group by 聚合函数,以pno字段分为1,2,3三类,然后求出它们三类中每一类中的几条数据volume字段的总和,
-- 最终显示三类三条数据,最后order by sum2 desc 排序降序,里面limit 2为前两名
select t.pno2 from (select pno as pno2,sum(volume) as sum2 from deal group by pno order by sum2 desc limit 2) t1
2.查询每种产品销售量最高的销售人员(使用group by关键字)?
select p.pname,t4.volume as maxvolume,t4.sname from(
select t3.pno,t3.volume,s2.sname from(
select * from deal d2 where d2.volume in(
select t2.maxs from(
select t1.pno,max(volume) as maxs from deal t1
group by t1.pno)t2
))t3
inner join sales s2 on s2.sno=t3.sno)t4
inner join poduction p on p.pno=t4.pno
-- t2:根据pno字段分类组,查询出当中volume最大的那几条数据
select t1.pno,max(volume) as maxs from deal t1
group by t1.pno
-- 只查询t2表中的maxs这一个字段
select t2.maxs from(
select t1.pno,max(volume) as maxs from deal t1
group by t1.pno)t2
-- t3:where..in 关键词,in()中只有一个字段,这个字段有三个值,在deal表中显示这三个值所在的那三行数据,
select * from deal d2 where d2.volume in(
select t2.maxs from(
select t1.pno,max(volume) as maxs from deal t1
group by t1.pno)t2
)
-- 连接t3和sales销售人员表,只显示其中需要的几个字段,比如pno字段用来连接poduction这个表
select t3.pno,t3.volume,s2.sname from(
select * from deal d2 where d2.volume in(
select t2.maxs from(
select t1.pno,max(volume) as maxs from deal t1
group by t1.pno)t2
))t3
inner join sales s2 on s2.sno=t3.sno
3.查询产品编号为2,且销售量超过100的销售人员的姓名及所在公司?
select t3.sname,a.aname from (
select s.sname,s.ano from(
select * from deal d where d.pno in(2) and d.volume>100)t1
inner join sales s on s.sno=t1.sno)t3
inner join agent a on a.ano=t3.ano;
-- t1 :查询出了deal表中产品编号为2,且销量超过100的那几条数据
select * from deal d where d.pno in(2) and d.volume>100;
-- t2:t1连接sales表,然后只显示出sname和ano字段
select s.sname,s.ano from(
select * from deal d where d.pno in(2) and d.volume>100)t1
inner join sales s on s.sno=t1.sno;
4.查询所有代理商所有产品的总销售量?
select t2.aname,sum(t2.volume) as '产品总销售量' from(
select d.volume,d.sno,t1.aname,t1.sname,d.pno from(
select a.ano,a.aname,s.sname,s.sno from agent a inner join sales s on s.ano=a.ano)t1
inner join deal d on d.sno=t1.sno)t2
group by t2.aname;
-- t1:agent表连接sales表
select a.ano,a.aname,s.sname,s.sno from agent a inner join sales s on s.ano=a.ano order by a.ano asc ;
5.查询每个产品分别有多少个销售人员在销售?
select p.pname,t1.rens as '在销售的人数' from(
select d.pno,count(sno) as rens from deal d group by d.pno)t1
inner join poduction p on p.pno=t1.pno
-- t1: 查出了三种产品分别有多少个人在销售
select d.pno,count(sno) from deal d group by d.pno;
6.查询名称包含BBB的代理商的所有销售人员?
select t1.aname,t1.sname from(
select s.sname,a.aname from sales s inner join agent a on a.ano=s.ano)t1
where t1.aname in('BBB公司');
-- t1:连接agent和sales表
select s.sname,a.aname from sales s inner join agent a on a.ano=s.ano;
7.查询总销售量最差的产品?
select p.pname,t1.sums,p.pdate from(
select d.pno,sum(d.volume) as sums from deal d group by d.pno order by sums asc limit 1)t1
inner join poduction p on t1.pno=p.pno;
8.查询2017年生产的产品的总销量?
select sum(volume) as sums from(
select t1.pname,d.pno,t1.pdate,d.volume from(
select * from poduction where year(pdate)=2017)t1
inner join deal d on t1.pno=d.pno)t2
-- t1:查询年份为2017年的数据
select * from poduction where year(pdate)=2017
-- t2:t1连接deal表,显示pno=1,2的数据
select t1.pname,d.pno,t1.pdate,d.volume from(
select * from poduction where year(pdate)=2017)t1
inner join deal d on t1.pno=d.pno;
#------------------------------------------------------------------------------#
以上8个问题所使用到的关键字使用例句
-- 内连接,表1和表2都有tid字段来连接
select * from table_1 t1 inner join table_2 t2 on t1.tid=t2.tid;
-- (表名 as 别名) 是给表取别名,,不像oracle中直接poduction.p,在mysql中得poduction as p这种格式取别名,例如
select * from poduction as p left join deal as d on p.pno=d.pno ;
-- 排序,升序asc,降序desc, limit 0,5 表示排名前五
select * from deal order by volume desc limit 0,5;
-- 计算表中一共有多少条数据
select count(*) from deal d;
-- where pno in (value,value),就是查询出deal表中字段为pno,它的值为1和3的数据
select * from deal where pno in(1,3)
-- group by 是聚合函数,所以使用的时候要用到count条数或sum求和数等等这些函数一起
-- deal表中有16条数据,以pno字段分为1,2,3三类,然后求出它们三类中每一类中的几条数据volume字段的总和,最终显示三类三条数据
select pno,sum(volume) from deal group by pno
-- order by 字段名 desc或asc ,降序或升序,limit 5 表示前五条
select * from deal order by volume desc limit 5
-- 修改poduction表中的pdate字段数据=2018-11-12为2017-05-25
update poduction set pdate='2017-05-25' where pdate='2018-11-12';