From 092b0f6013d0fbdbedbeb8bdd129a0580d967a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=98=E6=96=87=E8=AF=9A?= <3287861587@qq.com> Date: Sun, 17 Sep 2023 21:05:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230917 \345\260\217\346\265\213.md" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 "08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" "b/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" new file mode 100644 index 0000000..ba59b7d --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" @@ -0,0 +1,122 @@ +~~~ sql +CREATE DATABASE cat charset utf8; +use cat; +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023-09-15 08:56:05 */ +/*==============================================================*/ + + +drop table if exists car; + +drop table if exists client; + +drop table if exists salesman; + +drop table if exists salesrecord; + +/*==============================================================*/ +/* Table: car */ +/*==============================================================*/ +create table car +( + car_id char(10) not null, + car_brand char(20) not null, + car_model char(10) not null, + car_price decimal(10,1) not null, + primary key (car_id) +); + +INSERT into car VALUES +('s101','奔驰','e500',1000000), +('s102','奥迪','a6',800000), +('s103','法拉利','911',1563452), +('s104','五菱宏光','a100',234102); + +/*==============================================================*/ +/* Table: client */ +/*==============================================================*/ +create table client +( + client_id char(20) not null, + client_name varchar(20) not null, + client_sex char(2) not null, + client_age int not null, + client_tel varchar(12) not null, + primary key (client_id) +); + +INSERT into client VALUES +('1','时学安','男',23,'13161554256'), +('2','叶子豪','女',30,'14488796603'), +('3','周富','男',24,'16635696642'), +('4','陈佳炜','男',26,'14851123620'); + +/*==============================================================*/ +/* Table: salesman */ +/*==============================================================*/ +create table salesman +( + sales_id char(10) not null, + sales_name char(20) not null, + sales_sex char(2) not null, + sales_age int not null, + sales_tel varchar(12) not null, + primary key (sales_id) +); + +INSERT into salesman VALUES +('2','李俊兴','男',35,'14454756321'), +('1','谢铖浩','男',45,'13366522446'), +('3','肖钟凯韩','女',26,'15563320136'), +('4','马达','男',28,'14778952146'); + +/*==============================================================*/ +/* Table: salesrecord */ +/*==============================================================*/ +create table salesrecord +( + sal_quantity int not null, + sal_totalprices decimal(10,1) not null, + sal_time varchar(20) not null, + sal_id char(10) not null, + car_id char(10), + sales_id char(10), + client_id char(20), + primary key (sal_id) +); +INSERT into salesrecord VALUES +(1,1563452,'2022-02-03','1','s103','1','3'), +(1,234102,'2021-03-16','2','s104','3','2'), +(1,1000000,'2023-09-06','3','s101','4','1'), +(1,800000,'2023-12-23','4','s102','2','4'); + +alter table salesrecord add constraint FK_Relationship_1 foreign key (car_id) + references car (car_id) on delete restrict on update restrict; + +alter table salesrecord add constraint FK_Relationship_2 foreign key (sales_id) + references salesman (sales_id) on delete restrict on update restrict; + +alter table salesrecord add constraint FK_Relationship_3 foreign key (client_id) + references client (client_id) on delete restrict on update restrict; + + +-- 1.查询特定销售员的销售记录 +SELECT * FROM salesrecord where sales_id=(SELECT sales_id FROM salesman where sales_name='马达'); +-- 2.查找销售记录中销售价格最高的汽车 +SELECT MAX(car_price) FROM car; +SELECT * FROM car c LEFT JOIN salesrecord s on c.car_id=s.car_id where car_price=(SELECT MAX(car_price) FROM car); +-- 3.统计某个销售员的销售总额 +SELECT sum(sal_totalprices) FROM salesman sa LEFT JOIN salesrecord sl on sa.sales_id=sl.sales_id WHERE sales_name="马达" ; +-- 4.根据客户信息查询其购买过的汽车 + SELECT c.client_name,car.car_brand FROM client c LEFT JOIN salesrecord s on c.client_id=s.client_id LEFT JOIN car on car.car_id=s.car_id WHERE c.client_name ='周富'; +-- 5.分析特定品牌汽车的销售情况,统计每个品牌的销售数量和总销售额 +SELECT c.car_brand,COUNT(s.sal_quantity) 销售数量,SUM(s.sal_totalprices) 总销售额 FROM car c LEFT JOIN salesrecord s on c.car_id=s.car_id GROUP BY c.car_id; +-- 6.检索特定日期范围内的销售了哪些汽车 +SELECT * FROM salesrecord s LEFT JOIN car c on s.car_id=c.car_id where sal_totalprices BETWEEN '2023-09-06' and '2023-12-30'; +-- 7.查找某车型的销售历史。 +SELECT * FROM car c LEFT JOIN salesrecord s on c.car_id=s.car_id WHERE c.car_brand='法拉利'; +-- 8.统计每个销售员的销售数量 +SELECT c.client_name, COUNT(s.sal_quantity) FROM client c LEFT JOIN salesrecord s on c.client_id=s.client_id GROUP BY client_name; +~~~ + -- Gitee From e1a89adf01d3c90083aa0641fa72611fba9cde65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=98=E6=96=87=E8=AF=9A?= <3287861587@qq.com> Date: Sun, 17 Sep 2023 21:11:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230917 \345\260\217\346\265\213.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" "b/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" index ba59b7d..7f73037 100644 --- "a/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" +++ "b/08 \345\256\230\346\226\207\350\257\232/20230917 \345\260\217\346\265\213.md" @@ -1,4 +1,7 @@ +![image-20230917211022417](https://s2.loli.net/2023/09/17/5URPqvCK6tAkHmo.png) + ~~~ sql + CREATE DATABASE cat charset utf8; use cat; /*==============================================================*/ -- Gitee