各表关系
CUSTOMER(客户表)
列名 | 数据类型 | 为空 | 注释 |
---|---|---|---|
CID | char (11) | 否 | 客户号 |
Name | varchar2(16) | 否 | 姓名 |
Gender | Varchar2(3) | 是 | 性别 |
Rating | int | 否 | 等级分 |
Level | varchar2(20) | 否 | 用户等级 |
Orders
列名 | 数据类型 | 为空 | 注释 |
---|---|---|---|
OID | char (12) | 否 | 订单号 |
Order_times | date | 否 | 下单时间 |
CID | char(11) | 否 | 客户号 |
Total | number(10,2) | 否 | 订单金额 |
Order_Detail
列名 | 数据类型 | 为空 | 注释 |
---|---|---|---|
OID | char (12) | 否 | 订单号 |
No | int | 否 | 序号 |
GID | int | 否 | 货物编号 |
Num | int | 否 | 购买数量 |
Price | number(8,2) | 否 | 购买单价 |
1.查询姓名中含"勇"字的男性客户的客户号,姓名,用户等级(3分)
select CID,Name,grade
from customer
where Name like '%勇%' and Gender = 'male'
2.新增一个客户,客户号13173201234,姓名"张三",性别"男"(3分)
insert into customer (CID,name,gender)
valus('13173201234',张三,'男')
3.将"谢勇"所有的订单中的订单金额打八折(4分)
update orders
set total=total*0.8
where cid in (
select cid
from customer
where name = '谢勇' );
update orders a
set total=total*0.8
while exits (
select 1
from customer b
where a.id=b.id and b.name='谢勇');
4.删除从来没下过单的客户(4分)
delete from customer
where C_ID not in(
select C_ID
from orders)
5.建立一个试图V1(CID,Name,Gender,Total_Order,Count_Order),视图表示用户的所有订单的总额与订单总数,列分别为客户号,姓名,性别,订单总额,订单总数(只需要查询下过单的用户)(6分)
createview v1 as
select CID,Name,Gender,sum(total) as Total_Order ,count(*) as Count_Order
from customer natural join orders
group by CID,Name,Gender
6.将订单金额超过10000元的,进行每满500减100的优惠,订单金额超过5000元的进行每满500减50的优惠,
订单金额不超过5000元的进行每满500减30的优惠(6分)
update orders
set total =case
when total<=5000 then total -floor(total/500)*30
when total>5000 **(可以不要)**and total<=10000 then total - floor(total /500)*50
else total -floor(total/500) *100
end
7.查询每个客户订单额最大的一笔订单的订单号,下单时间、订单金额(6分)
关联子查询
select OID,CID,Order_times,Total
from Orders a
where Total = (
select max(total)
from Orders b
where a.cid=b.cid
)
select oid a.cid,total
from orders a,
(select cid,max(total) max_total from orders group by cid) b
where a.cid=b.cid and total = max_total
8.查询对于客户号为“13173201234”的客户所买过的所有商品,全部买过的客户的客户号和姓名。(6分)
with ta as
(select cid , gid
frome orders natural join order_detail)
select a.cid,a.name
from customer a
where not exists (
(select gid from ta where cid = '13173201234')
minus
(select gid from ta b where a.cid=b.cid)
)
两个not exists的做法
with ta as
(select cid , gid
frome orders natural join order_detail)
select a.cid,a.name
from customer a
where not exists (
select gid from ta c where cid = '13173201234'
and not exists (select 1 from ta b where a.cid=b.cid and c.gid = b.gid)
)
9.基于视图v1,使用标量子查询,查询男、女两类用户中订单总金额最多的前10位用户。(6分)
select * from(
select a.cid,a.name,a.gender,(select count(*)+1 from v1 b
where b.sum_total>a.sum_total and a.gender = b.gender) rk
from v1 a) where rk>=10
select a.cid,a.name,a.gender
from v1 a
where (select count(*)+1 from v1 b
where b.sun_total > a.sum_total and a.gender = b.gender)>=10
10.统计各用户等级中男性客户人数、女性客户人数和总人数。(6分)
交叉表查询(不确定是不是叫这个)
select grade,count(*) "总数",
sum (case gender when '男' then 1 else null end ) "男客户数"
sum (case gender when '女' then 1 else null end ) "女客户数"
from customer group by grade