今日学习
数据导入导出
- navicat导入导出方法
连接数据库账户
选择数据world
导入向导
导入类型选择excel
导入过程中可以修改字段类型
导入结果:
![MySQL打卡5-项目实战1_第1张图片](http://img.e-com-net.com/image/info8/480b80a584014e90b904f6ce36b28f3a.jpg)
导出步骤
选择数据库
选择表
导出向导
导出结果:
![MySQL打卡5-项目实战1_第2张图片](http://img.e-com-net.com/image/info8/ac48be367b144c7296d8b8b43f72bc75.jpg)
作业
项目七: 各部门工资最高的员工(难度:中等)
创建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 |
+------------+----------+--------+
CREATE TABLE Employee
(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Salary INT NOT NULL,
Departmentid INT NOT NULL
) ;
INSERT INTO Employee(Id,Name,Salary,Departmentid) VALUES (1,'Joe',70000,1);
INSERT INTO Employee(Id,Name,Salary,Departmentid) VALUES (2,'Henry',80000,2);
INSERT INTO Employee(Id,Name,Salary,Departmentid) VALUES (3,'Sam',60000,2);
INSERT INTO Employee(Id,Name,Salary,Departmentid) VALUES (4,'Max',90000,1);
CREATE TABLE Department (
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(50) NOT NULL );
INSERT INTO Department(Id,Name) VALUES (1,'IT');
INSERT INTO Department(Id,Name) VALUES (2,'Sales');
mysql> SELECT
-> b.Name AS Department,
-> a.Name AS Employee,
-> MAX(a.Salary) AS Salary
-> FROM Employee AS a
-> INNER JOIN Department AS b
-> ON a.Departmentid = b.Id
-> GROUP BY b.Name ;
+
| Department | Employee | Salary |
+
| IT | Joe | 90000 |
| Sales | Henry | 80000 |
+
2 rows in set (0.00 sec)
项目八: 换座位(难度:中等)
小美是一所中学的信息科技老师,她有一张 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 |
+---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
mysql> CREATE TABLE seat
-> (
-> id INT NOT NULL ,
-> student VARCHAR(50) NOT NULL
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO seat(id,student) VALUES (1,'Abbot');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO seat(id,student) VALUES (2,'Doris');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO seat(id,student) VALUES (3,'Emerson');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO seat(id,student) VALUES (4,'Green');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO seat(id,student) VALUES (5,'Jeames');
Query OK, 1 row affected (0.00 sec)
mysql> select max(id) as max_id from seat;
+
| max_id |
+
| 5 |
+
1 row in set (0.00 sec)
mysql> update seat set id =
-> case
-> when id = 5 then 5
-> when mod (id, 2) = 1 then id + 1
-> else id -1
-> end;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 5 Changed: 4 Warnings: 0
mysql> select * from seat;
+
| id | student |
+
| 2 | Abbot |
| 1 | Doris |
| 4 | Emerson |
| 3 | Green |
| 5 | Jeames |
+
5 rows in set (0.00 sec)
项目九: 分数排名(难度:中等)
编写一个 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 |
+-------+------+
mysql> CREATE TABLE scores (
-> Id INT NOT NULL ,
-> Score DECIMAL(10,2) NOT NULL
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (1,3.50);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (2,3.65);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (3,4.00);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (4,3.85);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (5,4.00);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO scores(Id,Score) VALUES (6,3.65);
Query OK, 1 row affected (0.01 sec)
mysql> select * from scores;
+
| Id | Score |
+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+
6 rows in set (0.00 sec)
mysql> SELECT
-> Score,
-> (SELECT count(DISTINCT score) FROM scores
-> WHERE Score >= s.score
-> ) AS '排名'
-> FROM
-> scores as s
-> ORDER BY
-> Score DESC;
+
| Score | 排名 |
+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+
6 rows in set (0.00 sec)