Datawhale MySQL 訓練營 Task5

數據導入導出

  • 導入table
    http://www.runoob.com/mysql/mysql-database-import.htmlhtml

  • 導出table
    http://www.runoob.com/mysql/mysql-database-export.htmlmysql

SELECT * FROM runoob_tbl 
INTO OUTFILE '/tmp/runoob.txt'

設置分隔符sql

SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

做業

  • 1.項目七: 各部門工資最高的員工(難度:中等)
    建立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 部門有最高工資。.net

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

解答:3d

CREATE TABLE employee(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Salary INT,
DepartmentId INT
);

INSERT INTO employee
(Id,Name,Salary,DepartmentId)
VALUES
(1,'Joe',70000,1),
(2,'Henry',80000,2),
(3,'Sam',60000,2),
(4,'Max',90000,2);

CREATE TABLE Department(
Id INT NOT NULL,
Name VARCHAR(20)
);


INSERT INTO Department
(Id,Name)
VALUES
(1,'IT'),
(2,'Sales');

SELECT Department.Name AS Department, employee.Name As Employee, employee.Salary AS Salary
FROM Department
JOIN employee
WHERE Department.Id = employee.DepartmentId
GROUP BY employee.DepartmentId
HAVING MAX(Salary)

結果:
code

  • 2.項目八: 換座位(難度:中等)
    小美是一所中學的信息科技老師,她有一張 seat 座位表,平時用來儲存學生名字和與他們相對應的座位 id。
    其中縱列的 id 是連續遞增的
    小美想改變相鄰倆學生的座位。
    你能不能幫她寫一個 SQL query 來輸出小美想要的結果呢?
    請建立以下所示seat表:
    示例:
+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

假如數據輸入的是上表,則輸出結果以下:htm

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

注意:
若是學生人數是奇數,則不須要改變最後一個同窗的座位。blog

解答:排序

CREATE TABLE seat(
id INT NOT NULL PRIMARY KEY,
student VARCHAR(20)
);

INSERT INTO seat
VALUES
(1,'Abbot'),
(2,'Doris'),
(3,'Emerson'),
(4,'Green'),
(5,'Jeames');


SELECT s.id,s.student from (
    SELECT id-1 as id, student 
    FROM seat
    WHERE id%2 = 0
        
    UNION
        
    SELECT id+1 as id, student
    FROM seat
    WHERE id%2 =1 AND id != (SELECT MAX(id) FROM seat)
        
    UNION 
        
    SELECT id,student
    FROM seat
    WHERE id%2=1 AND id = (SELECT MAX(id) FROM seat);
)s
ORDER BY s.id;

結果:

  • 3.項目九: 分數排名(難度:中等)
    編寫一個 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    |
+-------+------+
CREATE TABLE score(
id int,
score DECIMAL(3,2)
);

INSERT INTO score
VALUES
(1,3.50),
(2,3.65),
(3,4.00),
(4,3.85),
(5,4.00),
(6,3.65);

SELECT id, score,
CASE 
WHEN @pre_score = score THEN @rank_tmp
WHEN @pre_score:=score THEN @rank_tmp:=@rank_tmp+1
END AS 'rank'
FROM score,(SELECT @rank_tmp:=0,@pre_score:=NULL) tmp
ORDER BY
score.score DESC;

結果:

這裏用了變量和 CASE WHEN 語句,十分巧妙。
在MySQL 8.x 版本引入了窗口函數,包括 rank()、dense_rank();因此能夠這樣寫

SELECT
    id,
    score,
    dense_rank ( ) over ( ORDER BY score.score DESC ) AS 'rank' 
FROM
    score 
ORDER BY
    score DESC;

輕鬆優雅美滋滋。

參考

  • 排序部分參考了 https://www.jianshu.com/p/bb1b72a1623e
  • 換座位參考了 https://blog.csdn.net/wal1314520/article/details/80117658
  • 窗口函數參考了 https://blog.51cto.com/11103985/2341971
相關文章
相關標籤/搜索