From a36863daa20693e3ce035d4fe23a5cf35506913c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8E=E5=81=A5?= <2161737470@qq.com> Date: Sat, 16 Sep 2023 21:34:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\345\215\226\347\263\273\347\273\237.md" | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/20230916 \346\261\275\350\275\246\345\224\256\345\215\226\347\263\273\347\273\237.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20230916 \346\261\275\350\275\246\345\224\256\345\215\226\347\263\273\347\273\237.md" "b/04 \346\235\216\346\230\216\345\201\245/20230916 \346\261\275\350\275\246\345\224\256\345\215\226\347\263\273\347\273\237.md" new file mode 100644 index 0000000..1979a89 --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/20230916 \346\261\275\350\275\246\345\224\256\345\215\226\347\263\273\347\273\237.md" @@ -0,0 +1,154 @@ +![](https://s2.loli.net/2023/09/16/bYhJlVZ3SKnGOH7.png) + +~~~ mysql +create database buy_car charset utf8; +use buy_car; + +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023/9/16 16:12:55 */ +/*==============================================================*/ + + +drop table if exists car; + +drop table if exists car_brand; + +drop table if exists customer; + +drop table if exists sales_record; + +drop table if exists salesperson; + +/*==============================================================*/ +/* Table: car */ +/*==============================================================*/ +create table car +( + car_id int not null auto_increment, + cb_id int, + car_name varchar(10) not null, + car_price decimal(4,2) not null, + primary key (car_id) +); +insert into car values +(null,1,'奥迪A6L',42.79), +(null,1,'奥迪A8L',82.98), +(null,1,'奥迪A4L',32.18), +(null,2,'奔驰S400L',86.26), +(null,2,'奔驰GLA',28.78), +(null,2,'奔驰E级',50.25), +(null,3,'宝马X3',63.69), +(null,3,'宝马2系',41.98), +(null,3,'宝马X5',71.99); + +/*==============================================================*/ +/* Table: car_brand */ +/*==============================================================*/ +create table car_brand +( + cb_id int not null auto_increment, + cb_name varchar(10) not null, + primary key (cb_id) +); +insert into car_brand values +(null,'奥迪'), +(null,'奔驰'), +(null,'宝马'); + +/*==============================================================*/ +/* Table: customer */ +/*==============================================================*/ +create table customer +( + customer_id int not null auto_increment, + customer_name varchar(5) not null, + customer_sex char(1) not null, + customer_tel varchar(11) not null, + customer_address varchar(20) not null, + primary key (customer_id) +); +insert into customer values +(null,'张三','男','18850625585','福建省南平市'), +(null,'李四','女','16845766554','福建省龙岩市'), +(null,'王五','男','14950028523','福建省泉州市'), +(null,'赵六','女','15650258573','福建省漳州市'), +(null,'田七','女','14589562573','福建省漳州市'); + +/*==============================================================*/ +/* Table: 销售记录 */ +/*==============================================================*/ +create table sales_record +( + sr_id int not null auto_increment, + car_id int, + sp_id int, + customer_id int, + sr_time date not null, + primary key (sr_id) +); +insert into sales_record values +(null,2,1,1,'2023-02-13'), +(null,1,3,1,'2023-05-23'), +(null,3,1,3,'2023-03-19'), +(null,5,2,2,'2023-07-21'), +(null,4,4,5,'2023-06-14'), +(null,6,2,4,'2023-08-15'), +(null,9,3,2,'2023-07-21'); + +/*==============================================================*/ +/* Table: 销售员 */ +/*==============================================================*/ +create table salesperson +( + sp_id int not null auto_increment, + sp_name varchar(5) not null, + sp_sex char(1) not null, + sp_tel varchar(11) not null, + sp_address varchar(20) not null, + primary key (sp_id) +); +insert into salesperson values +(null,'小李','男','14648515156','福建省南平市'), +(null,'小敏','女','18956764856','福建省漳州市'), +(null,'小莉','女','15989525856','福建省龙岩市'), +(null,'小黄','男','15954879156','福建省龙岩市'); + +alter table car add constraint FK_belong_to foreign key (cb_id) + references car_brand (cb_id) on delete restrict on update restrict; + +alter table sales_record add constraint FK_buy foreign key (customer_id) + references customer (customer_id) on delete restrict on update restrict; + +alter table sales_record add constraint FK_sell_a foreign key (sp_id) + references salesperson (sp_id) on delete restrict on update restrict; + +alter table sales_record add constraint FK_sell_b foreign key (car_id) + references car (car_id) on delete restrict on update restrict; + + + + -- 1.查询特定销售员的销售记录(小李) + select * from salesperson s,sales_record sr where s.sp_id = sr.sp_id and sp_name = '小李'; + -- 2.查找销售记录中销售价格最高的汽车 + select * from car where car_price = + (select max(car_price) from car c,sales_record sr where c.car_id = sr.car_id); + -- 3.统计某个销售员的销售总额(小敏) + select sum(car_price) 万元 from car c,sales_record sr where c.car_id = sr.car_id and sp_id =(select sp_id from salesperson where sp_name = '小敏'); + -- 4.根据客户信息查询其购买过的汽车(李四) + select * from car where car_id in + (select car_id from sales_record where customer_id = (select customer_id from customer where customer_name = '李四')); + -- 5.分析特定品牌汽车的销售情况,统计每个品牌的销售数量和总销售额(奔驰) + select cb.cb_name 品牌,count(cb.cb_id) 销售数量,sum(car_price) 总销售额万元 + from car_brand cb,car c,sales_record sr where cb.cb_id = c.cb_id and c.car_id = sr.car_id group by cb.cb_id; + -- 6.检索特定日期范围内的销售了哪些汽车(2023-07-21) + select * from car where car_id in + (select car_id from sales_record where sr_time = '2023-07-21'); + -- 7.查找某车型的销售历史(宝马X3)。 + select * from sales_record where car_id = + (select car_id from car where car_name = '奔驰S400L'); + -- 8.统计每个销售员的销售数量 + select sp_name 销售员,count(sp.sp_id) 销售数量 from salesperson sp , sales_record sr where sp.sp_id = sr.sp_id group by sp.sp_id; + +~~~ + -- Gitee