From 6dfa31b81b1b0a1298b2a8bd369db47476302308 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 26 May 2022 20:40:43 +0800 Subject: [PATCH] union --- .../3.CASE/case.json" | 2 +- .../3.CASE/case.md" | 80 +++++++++++++++++ .../3.CASE/config.json" | 8 +- .../3.CASE/pivot.md" | 89 ------------------- .../6.UNION/config.json" | 8 +- .../6.UNION/union.json" | 7 ++ .../6.UNION/union.md" | 77 ++++++++++++++++ 7 files changed, 176 insertions(+), 95 deletions(-) rename "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.json" => "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.json" (83%) create mode 100644 "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.md" delete mode 100644 "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.md" create mode 100644 "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.json" create mode 100644 "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.md" diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.json" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.json" similarity index 83% rename from "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.json" rename to "data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.json" index e360651..232607b 100644 --- "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.json" +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.json" @@ -1,7 +1,7 @@ { "type": "code_options", "author": "ccat", - "source": "pivot.md", + "source": "case.md", "notebook_enable": false, "exercise_id": "d61198987832488ab53e8a18f7337946" } \ No newline at end of file diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.md" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.md" new file mode 100644 index 0000000..2a009e3 --- /dev/null +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/case.md" @@ -0,0 +1,80 @@ +# 透视表 + +Goods 表结构如下 + +```mysql +create table goods( + id int primary key auto_increment, + category_id int, + category varchar(64), + name varchar(256), + price decimal(12, 4), + stock int, + upper_time timestamp +) +``` + +Joe 想要做一个报表,只需要显示商品名和价格分级,其中不足10元的是 cheap, 超过1000的是expensive,其它的是 +normal,这个查询应该怎么写? + +## 答案 + +```mysql +select name, + case + when price < 10 then 'cheap' + when price > 1000 then 'expensive' + else 'normal' + end as level +from goods; +``` + +## 选项 + +### A + +```mysql +select name, + case price + when < 10 then 'cheap' + when > 1000 then 'expensive' + else 'normal' + end as level +from goods; +``` + +### B + +```mysql +select name, + case + when price < 10 'cheap' + when price > 1000 'expensive' + else 'normal' + end as level +from goods; +``` + +### C + +```mysql +select name, + case + when price < 10 then 'cheap' + when price > 1000 then 'expensive' + case _ 'normal' + end as level +from goods; +``` + +### C + +```mysql +select name, + case + when price < 10 then 'cheap' + when price > 1000 then 'expensive' + case _ 'normal' + end as level +from goods; +``` \ No newline at end of file diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/config.json" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/config.json" index 61478f6..d7cd4d3 100644 --- "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/config.json" +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/config.json" @@ -1,9 +1,13 @@ { "node_id": "mysql-8e6cd4d5f4b446a2bc3f5402de9bd49c", - "keywords": ["case", "pivot", "透视表"], + "keywords": [ + "case", + "pivot", + "透视表" + ], "children": [], "export": [ - "pivot.json" + "case.json" ], "keywords_must": [], "keywords_forbid": [], diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.md" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.md" deleted file mode 100644 index b1e6373..0000000 --- "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/3.CASE/pivot.md" +++ /dev/null @@ -1,89 +0,0 @@ -# 透视表 - -现有销售记录表 - -```mysql -create table sales( - id serial primary key , - sku_id integer not null , - amount decimal(12, 4), - created_at timestamp default now() -); -create index idx_created_at on sales(created_at); -``` - -现在我们希望对这个表做一个月度的透视汇总,得到2021年每个月每种商品(sku_id)的的销售总额,每个月一列,哪一项可以实现? - -## 答案 - -```mysql -select sku_id, - sum(case extract(month from created_at) when 1 then amount else 0 end) as Jan, - sum(case extract(month from created_at) when 2 then amount else 0 end) as Feb, - sum(case extract(month from created_at) when 3 then amount else 0 end) as Mar, - sum(case extract(month from created_at) when 4 then amount else 0 end) as Apr, - sum(case extract(month from created_at) when 5 then amount else 0 end) as May, - sum(case extract(month from created_at) when 6 then amount else 0 end) as June, - sum(case extract(month from created_at) when 7 then amount else 0 end) as July, - sum(case extract(month from created_at) when 8 then amount else 0 end) as Aug, - sum(case extract(month from created_at) when 9 then amount else 0 end) as Sept, - sum(case extract(month from created_at) when 10 then amount else 0 end) as Oct, - sum(case extract(month from created_at) when 11 then amount else 0 end) as Nov, - sum(case extract(month from created_at) when 12 then amount else 0 end) as Dec -from sales -where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp -group by sku_id; -``` - -## 选项 - -### 格式不相符 - -```mysql -select sku_id, extract(month from created_at) as month, sum(amount) -from sales -where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp -group by 1, 2; -``` - -### 计算逻辑错误 - -```mysql -select sku_id, - sum(amount) as Jan, - sum(amount) as Feb, - sum(amount) as Mar, - sum(amount) as Apr, - sum(amount) as May, - sum(amount) as June, - sum(amount) as July, - sum(amount) as Aug, - sum(amount) as Sept, - sum(amount) as Oct, - sum(amount) as Nov, - sum(amount) as Dec -from sales -where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp -group by sku_id, extract(month from created_at); -``` - -### 计算格式错误 - -```mysql -select sku_id, - sum(amount having extract(month from created_at) = 1) as Jan, - sum(amount having extract(month from created_at) = 2) as Feb, - sum(amount having extract(month from created_at) = 3) as Mar, - sum(amount having extract(month from created_at) = 4) as Apr, - sum(amount having extract(month from created_at) = 5) as May, - sum(amount having extract(month from created_at) = 6) as June, - sum(amount having extract(month from created_at) = 7) as July, - sum(amount having extract(month from created_at) = 8) as Aug, - sum(amount having extract(month from created_at) = 9) as Sept, - sum(amount having extract(month from created_at) = 10) as Oct, - sum(amount having extract(month from created_at) = 11) as Nov, - sum(amount having extract(month from created_at) = 12) as Dec -from sales -where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp -group by sku_id, extract(month from created_at); -``` \ No newline at end of file diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/config.json" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/config.json" index 035409f..fa37a4d 100644 --- "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/config.json" +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/config.json" @@ -1,9 +1,11 @@ { "node_id": "mysql-b57b6c08f5f240c6a997284e4448f088", - "keywords": [], + "keywords": ["union"], "children": [], - "export": [], + "export": [ + "union.json" + ], "keywords_must": [], "keywords_forbid": [], - "group": 0 + "group": 1 } \ No newline at end of file diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.json" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.json" new file mode 100644 index 0000000..b71330f --- /dev/null +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "ccat", + "source": "union.md", + "notebook_enable": false, + "exercise_id": "331f369b150340f9989ccea8abfcd42f" +} \ No newline at end of file diff --git "a/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.md" "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.md" new file mode 100644 index 0000000..1028f3b --- /dev/null +++ "b/data/2.MySQL\344\270\255\351\230\266/2. \346\237\245\350\257\242/6.UNION/union.md" @@ -0,0 +1,77 @@ +# Union + +现有员工信息表和顾客信息表如下 + +```mysql +create table employee( + id int primary key auto_increment, + name varchar(256), + address varchar(1024), + dept varchar(64) + -- ignore more +); + +create table customer( + id int primary key auto_increment, + name varchar(256), + address varchar(1024), + level int + -- ignore more +) + +``` + +Joe 需要员工和顾客的联系方式(姓名+地址)清单,用于邮寄礼品。这个查询如何写? + +## 答案 + +```mysql +select name, address +from customer +union +select name, address +from employee +``` + +## 选项 + +### A + +```mysql +select * +from customer +union +select * +from employee +``` + +### B + +```mysql +select * +from customer +join employee +``` + +### C + +```mysql +select * +from customer +join employee on customer.id = employee.id +``` + +### D + +```mysql +select * +from customer, employee +``` + +### E + +```mysql +select name, address +from customer, employee +``` + -- GitLab