#第二部分自作#1
select 公司名称,城市 from 供应商;
2
select g.供应商ID,g.公司名称
from 供应商 as g,产品 as c,类别 as l
where g.供应商ID=c.供应商ID
and c.类别ID=l.类别ID
and l.类别名称='海鲜';
3
select d.订单ID as 销售额前五订单ID(由大到小),y.运货商ID as 对应运货商
from 运货商 as y,订单 as d,订单明细 as dm
where d.订单ID=dm.订单ID
and d.运货商=y.运货商ID
order by (dm.单价*dm.数量*(1-dm.折扣))
limit 5;
4
select 产品名称
from 产品
where 单位数量 like '%箱%';
5
select 产品名称
from 产品 as c,供应商 as g
where c.供应商ID=g.供应商ID
and g.城市='重庆';
6
select d.订单ID
from 订单 as d,雇员 as g,订单明细 as dm
where d.雇员ID=g.雇员ID
and d.订单ID=dm.订单ID
and g.姓氏='郑'
and g.名字='建杰'
order by (dm.单价*dm.数量*(1-dm.折扣));
7
select c.产品名称,(dm.单价*dm.数量*(1-dm.折扣)) as 订单金额,y.公司名称
from 订单 as d,产品 as c,订单明细 as dm,运货商 as y
where d.订单ID=dm.订单ID
and dm.产品ID=c.产品ID
and d.运货商=y.运货商ID
and d.订单ID='10284';
8
select d.*,c.*
from 产品 as c,订单 as d,订单明细 as dm
where c.产品ID=dm.产品ID
and d.订单ID=dm.订单ID;
9
select d.订单ID,d.订购日期,k.公司名称,d.发货日期,(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
from 订单 as d,客户 as k,订单明细 as dm
where d.客户ID=k.客户ID
and d.订单ID=dm.订单ID
order by (dm.单价*dm.数量*(1-dm.折扣)) desc
limit 10;
10
select year(d.订购日期) as 年度,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
from 订单 as d,订单明细 as dm
where d.订单ID=dm.订单ID
group by year(d.订购日期);
11
drop view if exists newcompanyname;
create view newcompanyname as
select g.公司名称,count(*) as 总数
from 产品 as c,供应商 as g
where c.供应商ID=g.供应商ID
group by g.供应商ID;
select *
from newcompanyname
where 总数 =
(select 总数
from newcompanyname
order by 总数 desc
limit 1);
12
drop view if exists newclassproduct;
create view newclassproduct as
select lb.类别ID, count(*) as 包含产品数
from 产品 as cp,类别 as lb
where cp.类别ID=lb.类别ID
group by lb.类别ID;
select *
from newclassproduct
where 包含产品数=(
select 包含产品数 from newclassproduct
order by 包含产品数 desc
limit 1);
13
drop view if exists newtranname;
create view newtranname as
select y.公司名称,count(*) as 运货次数
from 订单 as d,运货商 as y
where d.运货商=y.运货商ID
group by y.运货商ID;
select *
from newtranname
where 运货次数 =
(select 运货次数
from newtranname
order by 运货次数 desc
limit 1);
14
select l.类别名称,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
from 类别 as l,订单明细 as dm,产品 as c
where c.类别ID=l.类别ID
and c.产品ID=dm.产品ID
group by l.类别ID;
select c.产品名称,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
from 产品 as c,订单明细 as dm
where c.产品ID=dm.产品ID
group by c.产品ID;
15
select d.*
from 订单 as d,产品 as c,订单明细 as dm,类别 as l
where d.订单ID=dm.订单ID
and c.类别ID=l.类别ID
and c.产品ID=dm.产品ID
and l.类别名称='海鲜'
order by sum(dm.单价*dm.数量*(1-dm.折扣)) desc
limit 1;
16
select year(d.订购日期) as 年份,quarter(d.订购日期) as 季度,
count(*) as 销售量
from 订单 as d,订单明细 as dm
where d.订单ID=dm.订单ID
group by year(d.订购日期),quarter(d.订购日期)
order by year(d.订购日期),quarter(d.订购日期);
17
select d.订单ID,k.客户ID,k.地区,(dm.单价*dm.数量*(1-dm.折扣)) as 订单总额
from 订单 as d,订单明细 as dm,客户 as k
where d.订单ID= dm.订单ID
and d.客户ID=k.客户ID
and (dm.单价*dm.数量*(1-dm.折扣)) >5000;
18
select distinct c.产品ID,c.产品名称
from 产品 as c,订单明细 as dm,订单 as d
where c.产品ID=dm.产品ID
and d.订单ID=dm.订单ID
group by year(d.订购日期),c.产品名称
having sum(dm.单价*dm.数量*(1-dm.折扣)) <2000;
19
select d.*
from 订单 as d
where d.订单ID like '102%';
20
select k.公司名称,d.*
from 订单 as d,客户 as k
where d.客户ID=k.客户ID
and k.公司名称 in ('中硕贸易','学仁贸易','正人资源','中通');
21
select *
from 订单 as d
where mod(month(d.订购日期),2)=0;
22
select dm.折扣,d.*
from 订单 as d,订单明细 as dm
where d.订单ID=dm.订单ID
and rtrim(dm.折扣)=0.15
union
select dm.折扣,d.*
from 订单 as d,订单明细 as dm
where d.订单ID=dm.订单ID
and rtrim(dm.折扣)=0.20;
23
select g.*
from 雇员 as g
where floor(datediff(date(g.雇用日期),date(g.出生日期))/365.2422)>30;
24
select l.类别名称,g.公司名称,c.*
from 产品 as c left join 类别 as l
on c.类别ID=l.类别ID
left join 供应商 as g
on c.供应商ID=g.供应商ID
where rtrim(c.单价)>30
order by c.产品ID;
25
select (c.单价*c.库存量) as 总额,c.*
from 产品 as c
order by c.单价*c.库存量;
26
select dm.*,gy.公司名称 as 供应商名称
from 订单明细 as dm,订单 as d,雇员 as g,
产品 as c,供应商 as gy
where dm.订单ID=d.订单ID
and dm.产品ID=c.产品ID
and d.雇员ID=g.雇员ID
and c.供应商ID=gy.供应商ID
and rtrim(g.职务)='销售代表'
and (dm.单价*dm.数量*(1-dm.折扣))<2000;
27
select distinct gy.公司名称 as 供应商,gy.城市 as 所在城市
from 订单 as dd,订单明细 as dm,客户 as kh,
产品 as cp,供应商 as gy
where kh.客户ID=dd.客户ID
and dd.订单ID=dm.订单ID
and dm.产品ID=cp.产品ID
and cp.供应商ID=gy.供应商ID
and rtrim(kh.公司名称)='艾德高科技';
28
select dd.订单ID,TimeStampDiff(day,dd.订购日期,dd.发货日期) as 发货期,
TimeStampDiff(day,dd.发货日期,dd.到货日期) as 运货期
from 订单 as dd
order by TimeStampDiff(day,dd.订购日期,dd.发货日期);
29
select distinct cp.*,yh.*
from 产品 as cp,运货商 as yh,订单 as dd,订单明细 as dm
where dd.订单ID=dm.订单ID
and dm.产品ID=cp.产品ID
and dd.运货商=yh.运货商ID
order by cp.产品ID;
30
select distinct concat(gy.姓氏,gy.名字) as 职工名称
from 雇员 as gy,订单 as dd,客户 as kh
where gy.雇员ID=dd.雇员ID
and dd.客户ID=kh.客户ID
and rtrim(gy.城市)='北京'
and kh.公司名称='福星制衣厂股份有限公司';
|