DW training camp Mysql database comb [five]
1 task
任务五
学习内容
数据导入导出 (见附件)
将Excel文件导入MySQL表
MySQL导出表到Excel文件
作业
项目七: 各部门工资最高的员工(难度:中等)
创建Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
创建Department 表,包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
项目八: 换座位(难度:中等)
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
请创建如下所示seat表:
示例:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
假如数据输入的是上表,则输出结果如下:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
项目九: 分数排名(难度:中等)
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
创建以下score表:
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
Data import and export 2
Annex excel file saved Baidu network disk: Accessories excel
2.1 The Excel file into MySQL table
To import an Excel file into Navicat for MySQL database
- The excel file type, the page table in the database to run 'import wizard', select the type of data to import:
- Select the Excel file, and select a data source file, and import the data source table, where I import xlsx fail, then save the file to import xlsx to xls:
- Selecting a target table, create a new table may be inserted into the data:
- Set the primary key
- Import mode selected, if a new table, selecting the first entry - add records to the destination table:
- Import success
- On the operating table
by the above steps to import excel file into Navicat database, the data table window, '+', and by '-' can be realized insert and delete a record, when to traverse each column add content, the use of 'the Tab 'down key to successively add data.
2.2 MySQL table Export Table to Excel file
- According to the table right choice Export Wizard;
- Select the table to export the data format, here we chose a table xls format;
- Next, select the table you want to export, and set the location for the exported file, and then click Next:
- Export success
- In the saved location, you can see the data table has been successfully exported
3 Job
3.1 the highest wages of employees of various departments
First create tables and insert data:
CREATE TABLE IF NOT EXISTS Employee(
Id SMALLINT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(20) NOT NULL,
Salary INT NOT NULL,
DepartmentId TINYINT NOT NULL
);
CREATE TABLE IF NOT EXISTS Department(
Id SMALLINT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(20) NOT NULL
);
INSERT Employee(Name,Salary,DepartmentId)
Values('Joe',70000,1),
('Henry',80000,2),
('Sam',60000,2),
('Max',90000,1);
INSERT Department(Name)
Values('IT'),
('Sales');
On the idea of dealing with this problem, I first connected first table, and then the connection table to set a good condition, the condition set for: the same corresponding id, and the use of sub-query filter out the maximum value to note here it is that because the topics are the final data table format, so then select the required name from the alias:
SELECT d.Name as Department,e.Name as Employee,e.Salary
FROM Employee e,Department d
WHERE e.DepartmentId = d.Id
and e.Salary = (SELECT MAX(Employee.Salary)
FROM Employee
WHERE Employee.DepartmentId = d.Id);
3.2 change seats
Similarly, we first create the table structure and insert data:
CREATE TABLE IF NOT EXISTS seat(
id TINYINT PRIMARY KEY AUTO_INCREMENT,
student VARCHAR(20) NOT NULL
);
INSERT seat(student)
VALUES('Abbot'),('Doris'),('Emerson'),('Green'),('Jeames');
Next, consider Swap adjacent seat, if it is odd, then the last seat without change, considered here is the case when using conditional statements:
SELECT (CASE
WHEN id=counts THEN id
WHEN MOD(id,2)=0 THEN id-1
ELSE id+1 END) AS id,student
FROM seat
,(SELECT COUNT(id) AS counts FROM seat)
ORDER BY id;
3.3 scores ranking
Write a SQL query to achieve the score rankings. If the two scores are identical, the two scores rank (Rank) the same.
First, create tables and insert statements:
-----创建表
CREATE TABLE IF NOT EXISTS score(
Id TINYINT PRIMARY KEY AUTO_INCREMENT,
Score FLOAT(5,2) NOT NULL
);
-----插入表
INSERT score(Score)
VALUES('3.50'),
('3.65'),
('4.00'),
('3.85'),
('4.00'),
('3.65');
The question now is how to select a score, and also select a score rankings? There is now an idea: the so-called current ranking, meaning substantially represents the current number is greater than or equal to its corresponding fraction, such as 4, 4 as well as the same score, then it should be a corresponding rank. So we need to find the score is greater than or equal to the fraction not repeated in the score table, and then in descending order:
SELECT Score,
(SELECT COUNT(DISTINCT Score)
FROM score AS s2
WHERE s2.Score>=s1.Score
)
FROM score AS s1
ORDER BY Score DESC;