网站源码是什么上海交通大学文科建设处网站
目录
- 1. 题目
- 1.1 函数练习
- 1.2 数据库查询
- 2. 解答
- 2.1 函数练习
- 2.2 数据库查询
1. 题目
1.1 函数练习
- 求圆周率的值,保留
6位小数。 - 生成两个
100到200间的随机数。 - 将”
武汉大学”,”数学学院”,”计算数学”连接成一个字符串。 - 求字符串中第三个字符为
A的所有字符串。 - 将‘
武汉大学计算机学院’的‘计算机’替换为‘数学’。 - 求‘
中国湖北武汉大学’中的学校名。 - 求‘
welcome to 中国’包含多少个字符和占多少字节数。 - 求今天到明年元旦还有多少天。
- 按“
yyyy年mm月dd日”的格式输出今天的日期。 - 将一英文单词
s改为首字母大写,其它字母小写。
1.2 数据库查询
准备工作,接下来要重新建立数据库 yggl:
drop database if exists yggl;create database if not exists yggl;USE yggl;drop table if exists departments;
CREATE TABLE Departments(DepartmentID char(3) NOT NULL PRIMARY KEY COMMENT '部门编号',DepartmentName char(20) NOT NULL COMMENT '部门名',Note text NULL COMMENT '备注'
);drop table if exists salary;
CREATE TABLE Salary(EmployeeID char(6) NOT NULL PRIMARY KEY COMMENT '员工编号',InCome float NOT NULL COMMENT '收入',OutCome float NOT NULL COMMENT '支出'
);drop table if exists employees;
CREATE TABLE Employees(EmployeeID char(6) NOT NULL PRIMARY KEY COMMENT '员工编号',Name char(10) NOT NULL COMMENT '姓名',Education char(4) NOT NULL COMMENT '学历',Birthday date NOT NULL COMMENT '出生日期',Sex char(2) NOT NULL COMMENT '性别',WorkYear tinyint(1) COMMENT '工作时间',Address char(20) NULL COMMENT '地址',PhoneNumber char(12) NULL COMMENT '电话号码',DepartmentID char(3) NOT NULL COMMENT '部门编号'
);insert into Departments values('1','财务部',null),('2','人力资源部',null),('3','经理办公室',null),('4','研发部',null),('5','市场部',null)
;insert into Employees values('000001','王林','大专','1966-1-23',1,8,'中山路32-1-508','83355668','2'),('010008','伍容华','本科','1976-3-28',1,3,'北京东路100-2','83321321','1'),('020010','王向蓉','硕士','1982-12-9',1,2,'四牌楼10-10-108','83792361','1'),('020018','李丽','大专','1960-7-30',0,6,'中山东路102-2','83413301','1'),('102201','刘明','本科','1972-10-18',1,3,'虎踞路100-2','83606608','5'),('102208','朱骏','硕士','1965-9-28',1,2,'牌楼巷5-3-106','84708817','5'),('108991','钟敏','硕士','1979-8-10',0,4,'中山路10-3-105','83346722','3'),('111006','张石兵','本科','1974-10-1',1,1,'解放路34-1-203','84563418','5'),('210678','林涛','大专','1977-4-2',1,2,'中山北路24-35','83467336','3'),('302566','李玉珉','本科','1968-9-20',1,3,'热河路209-3','58765991','4'),('308759','叶凡','本科','1978-11-18',1,2,'北京西路3-7-52','83308901','4'),('504209','陈林琳','大专','1969-9-3',0,5,'汉中路120-4-12','84468158','4')
;insert into Salary values('000001',2100.8,123.09),('010008',1582.62,88.03),('102201',2569.88,185.65),('111006',1987.01,79.58),('504209',2066.15,108),('302566',2980.7,210.2),('108991',3259.98,281.52),('020010',2860,198),('020018',2347.68,180),('308759',2531.98,199.08),('210678',2240,121),('102208',1980,100)
;
- 查询每个员工的所有数据,查询
Departments表和Salary表的所有数据; - 查询每个员工的姓名、地址和电话号码;
- 查询
Employees表中的部门号和性别,要求消除重复的行; - 查询
EmployeeID为000001的员工地址和电话; - 查询月收入高于
2000元的员工的员工号、姓名和收入; - 查询
1970年以后出生的员工的姓名和住址; - 查询财务部的所有员工的员工号和姓名;
- 查询
Employees表中女员工的地址和电话,并将标题分别设置为地址和电话; - 查询
Employees表中员工的姓名和性别,并且性别值为1时显示“男”,值为0时显示“女”; - 查询
Employees表中员工的姓名、住址和收入水平,收入水平:2000以下显示“低收入”,2000~3000元显示“中等收入”,3000元以上显示“高收入”; - 计算每个员工的实际收入,标题显示为“实际收入”,
实际收入=Income - Outcome; - 获取员工的人数;
- 计算
Salary表中员工月收入的平均值; - 计算
Salary表中所有员工的总收入; - 查询财务部员工的最高和最低实际收入;
- 查询姓“王”的员工的姓名和部门号;
- 查询员工号中倒数第
2个数字为0的员工的员工号和姓名; - 查询地址中含“中山”的员工的
ID和部门号; - 查询收入在
2000~3000间的员工的ID和姓名; - 查询部门号为
1或3的员工的ID和姓名。
2. 解答
2.1 函数练习
-
求圆周率的值,保留
6位小数。SELECT(PI()); -
生成两个
100到200间的随机数。SELECT RAND()*101+99; -
将”
武汉大学”,”数学学院”,”计算数学”连接成一个字符串。SELECT CONCAT("武汉大学","数学学院","计算数学"); -
求字符串中第三个字符为
A的所有字符串。SELECT * from stu1 where name LIKE '__A%'; -
将‘
武汉大学计算机学院’的‘计算机’替换为‘数学’。SELECT REPLACE("武汉大学计算机学院","计算机","数学"); -
求‘
中国湖北武汉大学’中的学校名。SELECT SUBSTR("中国湖北武汉大学",5,8); -
求‘
welcome to 中国’包含多少个字符和占多少字节数。SELECT CHAR_LENGTH("welcome to 中国"), LENGTH("welcome to 中国"); -
求今天到明年元旦还有多少天。
SELECT DATEDIFF(DATE("2024-01-01"),CURDATE()); -
按“
yyyy年mm月dd日”的格式输出今天的日期。SELECT DATE_FORMAT(CURDATE(),"%Y 年 %m 月 %d 日"); -
将一英文单词
s改为首字母大写,其它字母小写。set @s:="hello world!"; SELECT CONCAT(UPPER(LEFT(@s,1)),RIGHT(@s,CHAR_LENGTH(@s)-1));
2.2 数据库查询
准备工作,接下来要重新建立数据库 yggl:
drop database if exists yggl;create database if not exists yggl;USE yggl;drop table if exists departments;
CREATE TABLE Departments(DepartmentID char(3) NOT NULL PRIMARY KEY COMMENT '部门编号',DepartmentName char(20) NOT NULL COMMENT '部门名',Note text NULL COMMENT '备注'
);drop table if exists salary;
CREATE TABLE Salary(EmployeeID char(6) NOT NULL PRIMARY KEY COMMENT '员工编号',InCome float NOT NULL COMMENT '收入',OutCome float NOT NULL COMMENT '支出'
);drop table if exists employees;
CREATE TABLE Employees(EmployeeID char(6) NOT NULL PRIMARY KEY COMMENT '员工编号',Name char(10) NOT NULL COMMENT '姓名',Education char(4) NOT NULL COMMENT '学历',Birthday date NOT NULL COMMENT '出生日期',Sex char(2) NOT NULL COMMENT '性别',WorkYear tinyint(1) COMMENT '工作时间',Address char(20) NULL COMMENT '地址',PhoneNumber char(12) NULL COMMENT '电话号码',DepartmentID char(3) NOT NULL COMMENT '部门编号'
);insert into Departments values('1','财务部',null),('2','人力资源部',null),('3','经理办公室',null),('4','研发部',null),('5','市场部',null)
;insert into Employees values('000001','王林','大专','1966-1-23',1,8,'中山路32-1-508','83355668','2'),('010008','伍容华','本科','1976-3-28',1,3,'北京东路100-2','83321321','1'),('020010','王向蓉','硕士','1982-12-9',1,2,'四牌楼10-10-108','83792361','1'),('020018','李丽','大专','1960-7-30',0,6,'中山东路102-2','83413301','1'),('102201','刘明','本科','1972-10-18',1,3,'虎踞路100-2','83606608','5'),('102208','朱骏','硕士','1965-9-28',1,2,'牌楼巷5-3-106','84708817','5'),('108991','钟敏','硕士','1979-8-10',0,4,'中山路10-3-105','83346722','3'),('111006','张石兵','本科','1974-10-1',1,1,'解放路34-1-203','84563418','5'),('210678','林涛','大专','1977-4-2',1,2,'中山北路24-35','83467336','3'),('302566','李玉珉','本科','1968-9-20',1,3,'热河路209-3','58765991','4'),('308759','叶凡','本科','1978-11-18',1,2,'北京西路3-7-52','83308901','4'),('504209','陈林琳','大专','1969-9-3',0,5,'汉中路120-4-12','84468158','4')
;insert into Salary values('000001',2100.8,123.09),('010008',1582.62,88.03),('102201',2569.88,185.65),('111006',1987.01,79.58),('504209',2066.15,108),('302566',2980.7,210.2),('108991',3259.98,281.52),('020010',2860,198),('020018',2347.68,180),('308759',2531.98,199.08),('210678',2240,121),('102208',1980,100)
;
-
查询每个员工的所有数据,查询
Departments表和Salary表的所有数据;USE yggl;select * from employees; select * from departments; select * from salary; -
查询每个员工的姓名、地址和电话号码;
select name, Address, PhoneNumber from employees;注:细心的小伙伴肯定看见了一个需要修正的地方,即
name,之前说过name是一个关键字,虽然这里还是可以正常准确输出结果,但是不建议这么写哦~# 应该这么写 select `name`, Address, PhoneNumber from employees;注:另外在
SQL中,变量名、关键字是不区分大小写的! -
查询
Employees表中的部门号和性别,要求消除重复的行;select DISTINCT DepartmentID, Sex from employees; -
查询
EmployeeID为000001的员工地址和电话;select EmployeeID, Address, PhoneNumber from employees where EmployeeID = '000001'; -
查询月收入高于
2000元的员工的员工号、姓名和收入;select employees.EmployeeID, employees.`Name`, salary.InCome from employeesjoin salaryon employees.EmployeeID = salary.EmployeeID where salary.InCome > 2000;# 或者SELECT e.EmployeeID,Name,InCome FROM employees e join salary s on e.EmployeeID = s.EmployeeID where s.InCome>2000; -
查询
1970年以后出生的员工的姓名和住址;select `Name`, Birthday, Address from employees where year(Birthday) > 1970;# 或者SELECT Name, Birthday, Address FROM employees where Left(Birthday,4)>='1970'; -
查询财务部的所有员工的员工号和姓名;
# 已知财务部编号(不合实际) select EmployeeID, `Name` from employees where DepartmentID = 1;# 直接通过财务部名称来查询(符合实际) select EmployeeID, `Name` from employeesjoin departmentson employees.DepartmentID = departments.DepartmentID where DepartmentName = '财务部'; -
查询
Employees表中女员工的地址和电话,并将标题分别设置为地址和电话;select Address as 地址, PhoneNumber as 电话 from employees where sex = 0; -
查询
Employees表中员工的姓名和性别,并且性别值为1时显示“男”,值为0时显示“女”;# 方法一:【case】 select `Name`,casewhen Sex = '1' then '男'when Sex = '0' then '女'end as 性别 from employees;# 或者select `Name`,casewhen sex = '1' then '男'else '女' end as 性别 from employees;# 方法二: # 【if】(考虑数据中只有男女两种选择且要求不为空,所以没有判断0) select `Name`, if(Sex='1', '男', '女') as 性别 from employees;# 【有缺失值时】(本次数据不会发生这种情况) select `Name`, if(Sex='1', '男', if(Sex='0', '女', '未知')) as 性别 from employees; -
查询
Employees表中员工的姓名、住址和收入水平,收入水平:2000以下显示“低收入”,2000~3000元显示“中等收入”,3000元以上显示“高收入”;# 方法一:【case】 select employees.`Name`, employees.Address, casewhen salary.InCome < 2000 then '低收入'when salary.InCome > 3000 then '高收入'else '中等收入'end as 收入水平 from employees, salary where employees.EmployeeID = salary.EmployeeID;# 方法二:【if】 select employees.`Name`, employees.Address, if(salary.InCome < 2000, '低收入', if(salary.InCome > 3000, '高收入', '中等收入')) as 收入水平 from employees, salary where employees.EmployeeID = salary.EmployeeID; -
计算每个员工的实际收入,标题显示为“实际收入”,
实际收入=Income - Outcome;select employees.`Name`, salary.InCome - salary.OutCome as 实际收入 from employees, salary where employees.EmployeeID = salary.EmployeeID;# 或者设置一下小数位数为 2 # 方法一: SELECT e.EmployeeID, Name, format(sum(Income-Outcome),2) as 实际收入 FROM employees e join salary s on e.EmployeeID = s.EmployeeID group by e.EmployeeID ;# 方法二: select employees.`Name`, FORMAT(salary.InCome - salary.OutCome,2) as 实际收入 from employees, salary where employees.EmployeeID = salary.EmployeeID; -
获取员工的人数;
select count(*) as 人数 from employees; -
计算
Salary表中员工月收入的平均值;select AVG(InCome) as 员工月收入的平均值 from salary; -
计算
Salary表中所有员工的总收入;select SUM(InCome) as 所有员工的总收入 from salary; -
查询财务部员工的最高和最低实际收入;
# 已知财务部编号(不合实际) select MAX(salary.InCome) as 财务部员工的最高实际收入, MIN(salary.InCome) as 财务部员工的最低实际收入 from salaryjoin employeeson salary.EmployeeID = employees.EmployeeID where employees.DepartmentID = 1;# 直接通过财务部名称来查询(符合实际) select MAX(salary.InCome) as 财务部员工的最高实际收入, MIN(salary.InCome) as 财务部员工的最低实际收入 from employeesjoin salaryon salary.EmployeeID = employees.EmployeeIDjoin departmentson employees.DepartmentID = departments.DepartmentID where DepartmentName = '财务部'; -
查询姓“王”的员工的姓名和部门号;
select `Name`, DepartmentID from employees where `Name` like '王%';# 正则表达式 select `Name`, DepartmentID from employees where `Name` REGEXP '^王'; -
查询员工号中倒数第
2个数字为0的员工的员工号和姓名;# 若员工号是固定的且长度不长的话(显得笨拙) select EmployeeID, `Name` from employees where EmployeeID like '____0_';# 不论长度是否等长(很智能) select EmployeeID, `Name` from employees where EmployeeID like '%0_';# 正则表达式 select EmployeeID, `Name` from employees where EmployeeID REGEXP '0.$'; -
查询地址中含“中山”的员工的
ID和部门号;select EmployeeID, DepartmentID from employees where Address like '%中山%';# 正则表达式 select EmployeeID, DepartmentID from employees where Address REGEXP '中山'; -
查询收入在
2000~3000间的员工的ID和姓名;select employees.EmployeeID, employees.`Name`, Income from employeesjoin salaryon employees.EmployeeID = salary.EmployeeID where salary.InCome >= 2000 and salary.InCome <= 3000;# 或者用 between and select employees.EmployeeID, employees.`Name`, Income from employeesjoin salaryon employees.EmployeeID = salary.EmployeeID where salary.InCome BETWEEN 2000 and 3000; -
查询部门号为
1或3的员工的ID和姓名。select EmployeeID, `Name` from employees where DepartmentID='1' or DepartmentID='3';# 或者 select EmployeeID, `Name` from employees where DepartmentID in ('1','3');
上一篇文章:【数据库——MySQL】(8)表数据插入、修改和删除练习及讲解
下一篇文章:【数据库——MySQL】(10)视图和索引
