当前位置:网站首页>Day 5 of MySQL learning
Day 5 of MySQL learning
2022-07-06 09:40:00 【Rorschach379】
create table `tb_result` (
`rq` date not null,
`sf` char(1) not null
) engine=innodb;
insert into `tb_result` values
('2017-04-09',' - '),
('2017-04-09',' - '),
('2017-04-09',' negative '),
('2017-04-09',' negative '),
('2017-04-10',' - '),
('2017-04-10',' negative '),
('2017-04-10',' - ');
-- Use the above table to query the results as shown below .
-- | date | win | los |
-- | ---------- | ---- | ---- |
-- | 2017-04-09 | 2 | 2 |
-- | 2017-04-10 | 2 | 1 |
select
rq as `date`,
sum(case sf when ' - ' then 1 else 0 end) as win,
sum(case sf when ' negative ' then 1 else 0 end) as los
from tb_result
group by rq;
create table `tb_course`
(
`course_id` int unsigned not null comment ' Course number ',
`course_name` varchar(50) not null comment ' Course name ',
primary key (`course_id`)
) engine=innodb;
insert into `tb_course` values
(1, 'Python'),
(2, 'Java'),
(3, 'Database'),
(4, 'JavaScript');
drop table if exists `tb_open_date`;
create table `tb_open_date`
(
`month` char(6) not null comment ' Opening date ',
`course_id` int unsigned not null comment ' Course number '
) engine=innodb;
insert into `tb_open_date` values
('202106', 1),
('202106', 3),
('202106', 4),
('202107', 4),
('202108', 2),
('202108', 4);
-- Use the above table to query the results as shown below .
-- | course_name | Jun | Jul | Aug |
-- | ----------- | --- | --- | --- |
-- | Python | o | x | x |
-- | Java | x | x | o |
-- | Database | o | x | x |
-- | JavaScript | o | o | o |
select
`course_name`,
case when exists (
select 'x' from `tb_open_date` as t2
where t1.`course_id`=t2.`course_id` and `month`='202106'
) then 'o' else 'x' end as Jun,
case when exists (
select 'x' from `tb_open_date` as t2
where t1.`course_id`=t2.`course_id` and `month`='202107'
) then 'o' else 'x' end as Jul,
case when exists (
select 'x' from `tb_open_date` as t2
where t1.`course_id`=t2.`course_id` and `month`='202108'
) then 'o' else 'x' end as Aug
from `tb_course` as t1;
create database homework default charset utf8mb4;
use homework;
create table tb_product
(
prod_id varchar(50) not null comment ' Commodity number ',
category varchar(50) not null comment ' species ',
color varchar(10) not null comment ' Color ',
weight decimal(10, 2) not null comment ' weight ',
price integer not null comment ' Price ',
primary key (prod_id)
) engine=innodb comment ' Product list ';
insert into tb_product values
('prodA', 'cateA', 'yellow', 5.6, 100),
('prodB', 'cateB', 'red', 3.7, 200),
('prodC', 'cateC', 'blue', 10.3, 300),
('prodD', 'cateD', 'black', 7.8, 400);
create table tb_order
(
id integer not null auto_increment,
order_no varchar(20) not null comment ' The order number ',
user_id varchar(50) not null comment ' The user no. ',
order_date date not null comment ' Order date ',
store varchar(50) not null comment ' Shop number ',
product varchar(50) not null comment ' Commodity number ',
quantity integer not null comment ' Purchase quantity ',
primary key (id)
) engine=innodb comment ' The order sheet ';
insert into tb_order
(order_no, user_id, order_date, store, product, quantity)
values
('D001', 'customerA', '2018-01-01', 'storeA', 'prodA', 1),
('D001', 'customerA', '2018-01-01', 'storeA', 'prodB', 1),
('D001', 'customerA', '2018-01-01', 'storeA', 'prodC', 1),
('D002', 'customerB', '2018-01-12', 'storeB', 'prodB', 1),
('D002', 'customerB', '2018-01-12', 'storeB', 'prodD', 1),
('D003', 'customerC', '2018-01-12', 'storeC', 'prodB', 1),
('D003', 'customerC', '2018-01-12', 'storeC', 'prodC', 1),
('D003', 'customerC', '2018-01-12', 'storeC', 'prodD', 1),
('D004', 'customerA', '2018-01-01', 'storeD', 'prodD', 2),
('D005', 'customerB', '2018-01-23', 'storeB', 'prodA', 1);
create table tb_store
(
store_id varchar(50) not null comment ' Shop number ',
city varchar(20) not null comment ' City ',
primary key (store_id)
) engine=innodb comment ' Shop list ';
insert into tb_store values
('storeA', 'cityA'),
('storeB', 'cityA'),
('storeC', 'cityB'),
('storeD', 'cityC'),
('storeE', 'cityD'),
('storeF', 'cityB');
-- Find out that the total purchase amount is not less than 800 User number of user 、 Total purchase amount 、 Total orders and total purchases , If the order number is the same, it will be counted as one order .
select
user_id,
sum(price*quantity) as Total purchase amount ,
count(distinct order_no) as Total order number ,
sum(quantity) as Total number of goods purchased
from tb_order, tb_product
where product=prod_id
group by user_id
having Total purchase amount >=800;
select
user_id,
sum(price*quantity) as Total purchase amount ,
count(distinct order_no) as Total order number ,
sum(quantity) as Total number of goods purchased
from (
select user_id, price, quantity, order_no
from tb_order inner join tb_product
on product=prod_id
) temp
group by user_id
having Total purchase amount >=800;
-- Find out all the cities ( Including cities without purchase records ) The number of head offices , Total number of buyers and total purchase amount .
select
city,
count(distinct store_id) as Number of head office stores ,
count(distinct user_id) as Total number of buyers ,
coalesce(sum(price*quantity), 0) as Total purchase amount
from tb_store
left outer join tb_order on store=store_id
left outer join tb_product on product=prod_id
group by city;
-- Find out if you have bought "cateA" Users of products and their average order amount , If the order number is the same, it will be counted as one order .
select
user_id,
sum(price*quantity) / count(distinct order_no) as Average order amount
from tb_order, tb_product
where product=prod_id and user_id in (
select
distinct user_id
from tb_order, tb_product
where product=prod_id and category='cateA'
)
group by user_id;
边栏推荐
- Lua script of redis
- Segmentation sémantique de l'apprentissage profond - résumé du code source
- 五月刷题01——数组
- 五月刷题26——并查集
- Vs All comments and uncomments
- Redis之Geospatial
- Kratos ares microservice framework (I)
- Redis之核心配置
- Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
- 基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
猜你喜欢
Redis cluster
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
Kratos ares microservice framework (I)
Redis之哨兵模式
解决小文件处过多
数据建模有哪些模型
Redis之发布订阅
Hero League rotation map automatic rotation
QDialog
Withdrawal of wechat applet (enterprise payment to change)
随机推荐
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
一文读懂,DDD落地数据库设计实战
Activiti7工作流的使用
018. Valid palindromes
数据建模有哪些模型
英雄联盟轮播图自动轮播
【深度学习】语义分割-源代码汇总
Redis之主从复制
【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need
五月刷题02——字符串
五月刷题03——排序
小白带你重游Spark生态圈!
Redis cluster
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Why data Tiering
tn-c为何不可用2p断路器?
美团二面:为什么 Redis 会有哨兵?
Redis之核心配置
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)