Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_mysql
提交
6dfa31b8
S
skill_tree_mysql
项目概览
CSDN 技术社区
/
skill_tree_mysql
通知
21
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_mysql
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6dfa31b8
编写于
5月 26, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
union
上级
fd30a1a2
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
180 addition
and
96 deletion
+180
-96
data/2.MySQL中阶/2. 查询/3.CASE/case.json
data/2.MySQL中阶/2. 查询/3.CASE/case.json
+2
-1
data/2.MySQL中阶/2. 查询/3.CASE/case.md
data/2.MySQL中阶/2. 查询/3.CASE/case.md
+81
-0
data/2.MySQL中阶/2. 查询/3.CASE/config.json
data/2.MySQL中阶/2. 查询/3.CASE/config.json
+6
-2
data/2.MySQL中阶/2. 查询/3.CASE/pivot.md
data/2.MySQL中阶/2. 查询/3.CASE/pivot.md
+0
-90
data/2.MySQL中阶/2. 查询/6.UNION/config.json
data/2.MySQL中阶/2. 查询/6.UNION/config.json
+6
-3
data/2.MySQL中阶/2. 查询/6.UNION/union.json
data/2.MySQL中阶/2. 查询/6.UNION/union.json
+8
-0
data/2.MySQL中阶/2. 查询/6.UNION/union.md
data/2.MySQL中阶/2. 查询/6.UNION/union.md
+77
-0
未找到文件。
data/2.MySQL中阶/2. 查询/3.CASE/
pivot
.json
→
data/2.MySQL中阶/2. 查询/3.CASE/
case
.json
浏览文件 @
6dfa31b8
{
{
"type"
:
"code_options"
,
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"author"
:
"ccat"
,
"source"
:
"
pivot
.md"
,
"source"
:
"
case
.md"
,
"notebook_enable"
:
false
,
"notebook_enable"
:
false
,
"exercise_id"
:
"d61198987832488ab53e8a18f7337946"
"exercise_id"
:
"d61198987832488ab53e8a18f7337946"
}
}
\ No newline at end of file
data/2.MySQL中阶/2. 查询/3.CASE/case.md
0 → 100644
浏览文件 @
6dfa31b8
# 透视表
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
data/2.MySQL中阶/2. 查询/3.CASE/config.json
浏览文件 @
6dfa31b8
{
{
"node_id"
:
"mysql-8e6cd4d5f4b446a2bc3f5402de9bd49c"
,
"node_id"
:
"mysql-8e6cd4d5f4b446a2bc3f5402de9bd49c"
,
"keywords"
:
[
"case"
,
"pivot"
,
"透视表"
],
"keywords"
:
[
"case"
,
"pivot"
,
"透视表"
],
"children"
:
[],
"children"
:
[],
"export"
:
[
"export"
:
[
"
pivot
.json"
"
case
.json"
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[],
"keywords_forbid"
:
[],
...
...
data/2.MySQL中阶/2. 查询/3.CASE/pivot.md
已删除
100644 → 0
浏览文件 @
fd30a1a2
# 透视表
现有销售记录表
```
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
data/2.MySQL中阶/2. 查询/6.UNION/config.json
浏览文件 @
6dfa31b8
{
{
"node_id"
:
"mysql-b57b6c08f5f240c6a997284e4448f088"
,
"node_id"
:
"mysql-b57b6c08f5f240c6a997284e4448f088"
,
"keywords"
:
[],
"keywords"
:
[
"union"
],
"children"
:
[],
"children"
:
[],
"export"
:
[],
"export"
:
[
"union.json"
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[],
"keywords_forbid"
:
[],
"group"
:
0
"group"
:
1
}
}
\ No newline at end of file
data/2.MySQL中阶/2. 查询/6.UNION/union.json
0 → 100644
浏览文件 @
6dfa31b8
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"union.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"331f369b150340f9989ccea8abfcd42f"
}
\ No newline at end of file
data/2.MySQL中阶/2. 查询/6.UNION/union.md
0 → 100644
浏览文件 @
6dfa31b8
# 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
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录