当前位置:网站首页>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;
边栏推荐
- QML control type: Popup
- Redis之主从复制
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- The order of include header files and the difference between double quotation marks "and angle brackets < >
- YARN组织架构
- [deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
- Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
- 面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
- Yarn organizational structure
- Mapreduce实例(四):自然排序
猜你喜欢
MapReduce instance (IV): natural sorting
O & M, let go of monitoring - let go of yourself
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Mapreduce实例(七):单表join
软件负载均衡和硬件负载均衡的选择
工作流—activiti7环境搭建
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
DCDC power ripple test
为拿 Offer,“闭关修炼,相信努力必成大器
Redis geospatial
随机推荐
Hero League rotation map automatic rotation
Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
MapReduce instance (VIII): Map end join
一大波开源小抄来袭
May brush question 27 - figure
小白带你重游Spark生态圈!
CAP理论
Mapreduce实例(六):倒排索引
一文读懂,DDD落地数据库设计实战
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
Mapreduce实例(十):ChainMapReduce
Mapreduce实例(四):自然排序
QDialog
【深度学习】语义分割-源代码汇总
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
Persistence practice of redis (Linux version)
英雄联盟轮播图手动轮播
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Use of activiti7 workflow