概述
内容:
1. MySQL项目
1.1 数据的导入和导出
1.2 代码
2. 其他复杂项目
1. MySQL项目
1.1 数据的导入和导出
以前文所创建的world表为例,导出到CSV文件到桌面的SQL文件夹,文件名为world.csv。
SELECT * FROM world INTO OUTFILE '//Mac/Home/Desktop/SQL/world.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY 'rn';
导入的文件命名为world2。
LOAD DATA INFILE '//Mac/Home/Desktop/SQL/world.csv' INTO TABLE world2 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY 'n' IGNORE 1 ROWS;
1.2 代码
项目七: 各部门工资最高的员工(难度:中等)
创建 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 部门有最高工资。
CREATE TABLE Employee( Id INT NOT NULL AUTO_INCREMENT , NAME VARCHAR(25) NOT NULL, Salary INT NOT NULL, DepartmentId INT NOT NULL, PRIMARY KEY (Id) ); INSERT INTO Employee (Name,Salary,DepartmentId) VALUES('Joe',70000,1), ('Henry',80000,2), ('Sam',60000,2), ('Max',90000,1);
CREATE TABLE Department( Id INT NOT NULL AUTO_INCREMENT , NAME VARCHAR(25) NOT NULL, PRIMARY KEY (Id) ); INSERT INTO Department (Id,Name) VALUES(1,'IT'), (2,'Sales'sample);
SELECT D.Name AS Department, E.NAME AS Employee,E.Salary FROM employee E, (SELECT DepartmentID, MAX(Salary) AS MAX FROM Employee GROUP BY DepartmentId) T, Department D WHERE E.DepartmentId=T.DepartmentId AND E.Salary=T.MAX AND E.DepartmentId=D.Id
项目八: 换座位(难度:中等)
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位
id。
其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
假如数据输入的是上表,则输出结果如下:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
SELECT @id:= @id +1 AS id, student FROM (SELECT case when MOD(id,2)=0 then id -1 when MOD(id,2) <>0 then id+1 AS id, student FROM seat ORDER BY id) AS new_seat, (SELECT @id :=0) AS init;
项目九: 分数排名(难度:中等)
编写一个 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 |
+-------+------+
SELECT Score, @rank:=@rank+(@prev <> (@prev := Score)) Rank FROM Scores, (SELECT @rank := 0, @prev := -1) init ORDER BY Score DESC;
2. 复杂项目
项目十:行程和用户
SELECT Trips.request_at AS DAY, ROUND(COUNT(case Trips.STATUS when 'Completed' then NULL ELSE 1 END) /COUNT(*),2) AS 'Cancellation Rate' FROM Users JOIN Trips ON Users.Users_id=Trips.Client_id WHERE Users.Banned ='No' AND Trips.request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY Trips.request_at;
项目十一:各部门前3高工资的员工
SELECT Department, Employee, Salary FROM( SELECT Department, Employee, Salary, (case when @prevd = (@prevd := Department) AND @prevs <> (@prevs := salary) then @c := @c+1 when @prevd =(@prevd : Department) AND @prevs =(@prevs := salary) then @c ELSE @c :=1 END) AS c FROM ( SELECT d.NAME AS Department, e.NAME AS Employee, Salary FROM Employee AS e JOIN Department AS d ON e.departmentid=d.id ORDER BY d.NAME,salary DESC) AS t JOIN (SELECT @c :=1, @prevd :=0, @prevs :=0) AS init ) AS t2 WHERE t2.c <=3;
转载于:https://www.cnblogs.com/statlearning2019/p/10660775.html
最后
以上就是壮观猫咪为你收集整理的MySQL(II) 任务四的全部内容,希望文章能够帮你解决MySQL(II) 任务四所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复