当前位置:网站首页>自动化办公案例:如何自动生成期数据?
自动化办公案例:如何自动生成期数据?
2022-07-31 02:27:00 【猴子数据分析】

【面试题】
有一张“学生学期信息表”,包含4个字段:学生id、学生姓名、学期、班级名称。

【问题】目前只有高一上学期的信息,需要自动填充完剩余学期的信息。
1)学生在升学的过程中不换班,例如:高一念了1班后,高二高三也是1班;
2)2013-2014-1 代表上学期,2013-2014-2 代表下学期。
【解题步骤】
1. 问题分析
学生的学期信息规律性很强,根据已有的一条数据,我们很容易写出“张三”高中剩余五个学期对应的信息。
那么,如何根据高一上学期的信息,在MySQL中自动生成剩余学期的信息?
我们只要使用几种字符串函数,以及UNION操作符即可。
2. 函数介绍
1)字符串替换函数REPLACE:
REPLACE(字符串,被替换字符串,替换字符串)该函数可以解释为:在“字符串”中出现的“被替换字符串”均被“替换字符串”替换,然后返回替换后的字符串。
2)UNION操作符:
MySQL的UNION操作符用于连接两个及以上的SELECT语句的结果组合到一个结果集合中,同时会删除重复的数据。而使用UNION ALL则不会删除重复数据。

3. 获取结果
1)高一上学期:
select *
from 学生学期信息表;返回结果为:

2)高一下学期:
由于直接替换“1”会将所有“1”换掉,所以我们将“2013-2014-1”中的“-1”替换成“-2”。
select *
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(学期,'-1','-2') as 学期,
班级名称
from 学生学期信息表;返回结果为:

3)高二上学期:
替换年份时,如果先将“2013”换成“2014”,再去替换“2014”时,会将两个“2014”都替换掉,所以我们先将“2014”替换成“2015”。
select *
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(学期,'-1','-2') as 学期,
班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(学期,'2014','2015'),'2013','2014') as 学期,
REPLACE(班级名称,'一','二') as 班级名称
from 学生学期信息表;返回结果为:

3)高二下学期:
在“高二上学期”的基础上,将“-1”替换成“-2”。
select *
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(学期,'-1','-2') as 学期,
班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(学期,'2014','2015'),'2013','2014') as 学期,
REPLACE(班级名称,'一','二') as 班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(REPLACE(学期,'2014','2015'),'2013','2014'),'-1','-2') as 学期,
REPLACE(班级名称,'一','二') as 班级名称
from 学生学期信息表;返回结果为:

4)高三上&下学期:
按照以上方法,最后汇总高三学期信息。
select *
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(学期,'-1','-2') as 学期,
班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(学期,'2014','2015'),'2013','2014') as 学期,
REPLACE(班级名称,'一','二') as 班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(REPLACE(学期,'2014','2015'),'2013','2014'),'-1','-2') as 学期,
REPLACE(班级名称,'一','二') as 班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(学期,'2014','2016'),'2013','2015') as 学期,
REPLACE(班级名称,'一','三') as 班级名称
from 学生学期信息表
union
select 学生id,
学生姓名,
REPLACE(REPLACE(REPLACE(学期,'2014','2016'),'2013','2015'),'-1','-2') as 学期,
REPLACE(班级名称,'一','三') as 班级名称
from 学生学期信息表;返回结果为:

【本题考点】
1)考查对字符串函数的了解;
2)考查对UNION操作符的了解。


️点击「阅读原文」
免费报名 数据分析训练营
边栏推荐
- 【shell基础】判断目录是否为空
- 数学解决——环形链表问题
- There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
- Observer mode (1)
- Fiddler captures packets to simulate weak network environment testing
- Layer 2 broadcast storm (cause + judgment + solution)
- 基于FPGA的图像实时采集
- vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
- 完整复制虚拟机原理(云计算)
- Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
猜你喜欢

multiplayer-hlap 包有问题,无法升级的解决方案

Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue

1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance

The effective square of the test (one question of the day 7/29)

怎样做好一个创业公司CTO?

19.支持向量机-优化目标和大间距直观理解

【Bank Series Phase 1】People's Bank of China

Nacos

Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?

Intranet Infiltration - Privilege Escalation
随机推荐
Face detection based on opencv
mysql 索引
完整复制虚拟机原理(云计算)
Nacos
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
ShardingJDBC使用总结
医疗影像领域AI软件开发流程
Difference between CMOS and TTL?
Huawei od dice js
[1153]mysql中between的边界范围
User interaction + formatted output
leetcode-1161: Maximum in-layer element sum
Crypto Life, a day in the life of a Web3 project partner
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
The difference between link and @import
静态路由+PAT+静态NAT(讲解+实验)
934. The Shortest Bridge
Shell script to loop through values in log file to sum and calculate average, max and min
AtCoder Beginner Contest 261 Partial Solution