導入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;
+----+-------+--------+--------------+ | 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
+---------+---------+ | 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;
結果:
+----+-------+ | 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;
輕鬆優雅美滋滋。