MySQL 4

作者: 假如时光不完美 | 来源:发表于2019-04-05 08:34 被阅读0次

    4.1 MySQL 实战

    感谢 https://blog.csdn.net/u011490595/article/details/82050325

    让我终于连接上了navicat,终于不用看性冷淡的命令行了。

    #学习内容#

    数据导入导出

    将之前创建的任意一张MySQL表导出,且是CSV格式

    再将CSV表导入数据库

    项目七: 各部门工资最高的员工(难度:中等)

    创建 Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id.

    创建 Department 表,包含公司所有部门的信息。

    编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。(也可以使用RANK函数)

    SELECT d. NAME AS Department, e. NAME AS Employee, e.Salary FROM Employee e

    JOIN (SELECT Max(Salary) AS Salary FROM Employee GROUP BY DepartmentId)

     AS g  ON  e.Salary = g.Salary LEFT JOIN Department d ON  e.DepartmentId = d.Id;

    项目八: 换座位(难度:中等)

    小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

    其中纵列的 id 是连续递增的

    小美想改变相邻俩学生的座位。如果学生人数是奇数,则不需要改变最后一个同学的座位。

    SELECT (CASE WHEN MOD(id,2)=1 AND id != (SELECT COUNT(*)FROM Seat) THEN id+1

    WHEN MOD(id,2)=0 THEN id-1 ELSE id END) AS id, student From Seat

    ORDER BY id ASC;

    项目九: 分数排名(难度:中等)

    编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

    SELECT Score,(SELECT COUNT(*) FROM (SELECT DISTINCT Score S FROM score) AS s2

    WHERE  S >= Score) AS 'Rank' FROM score ORDER BY score DESC;

    项目十:行程和用户(难度:困难)

    Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。 enum可以用来约束输入的值。

    CREATE TABLE Trips (Id INT auto_increment PRIMARY KEY, Client_Id INT NOT NULL, Driver_Id INT NOT NULL, City_Id INT NOT NULL, Status ENUM ('completed','cancelled_by_driver','cancelled_by_client'),Request_all DATE NOT NULL);

    INSERT INTO Trips VALUES (NULL,1,10,1,'completed','2013-10-1');

    INSERT INTO Trips VALUES (NULL,2,11,1,'cancelled_by_driver','2013-10-01'),

    (NULL,3,12,6,'completed','2013-10-01'),(NULL,4,13,6,'cancelled_by_client','2013-10-01'),(NULL,1,10,1,'completed','2013-10-02'),

    (NULL,2,11,6,'completed','2013-10-02'),(NULL,3,12,6,'completed','2013-10-02'),

    (NULL,2,12,12,'completed','2013-10-03'),(NULL,3,10,12,'completed','2013-10-03'),(NULL,4,13,12,'cancelled_by_driver','2013-10-03');

    Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

    CREATE TABLE Users (Users_Id INT NOT NULL PRIMARY KEY, Banned enum('Yes','No'), Role enum('client','driver','partner'));

    INSERT INTO Users VALUES (1,'No','client'),(2,'Yes','client'),(3,'No','client'),(4,'No','client'), (10,'No','driver'),(11,'No','driver'),(12,'No','driver'),(13,'No','driver');

    SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM

    (SELECT Name,Salary, DepartmentId,

    RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS 'rank'

    FROM Employee) AS e

    LEFT JOIN Department d ON e.DepartmentId=d.Id WHERE e.rank<=3;

    SELECT t.Request_all AS Day, ROUND(SUM(CASE WHEN Status ='completed' THEN 0 ELSE 1 END)/COUNT(*),2) AS 'Cancellation Rate'

    FROM Trips t LEFT JOIN Users U ON t.Client_Id=U.Users_Id LEFT JOIN Users U2 on t.Driver_Id=U2.Users_Id

    where U.Banned='No' AND U2.Banned='No' AND t.Request_all BETWEEN '2013-10-01'and'2013-10-03' GROUP BY t.Request_all;

    项目十一:各部门前3高工资的员工(难度:中等)

    将项目7中的 employee 表清空,重新插入以下数据(其实是多插入5,6两行):

    +----+-------+--------+--------------+

    | Id | Name  | Salary | DepartmentId |

    +----+-------+--------+--------------+

    | 1  | Joe  | 70000  | 1            |

    | 2  | Henry | 80000  | 2            |

    | 3  | Sam  | 60000  | 2            |

    | 4  | Max  | 90000  | 1            |

    | 5  | Janet | 69000  | 1            |

    | 6  | Randy | 85000  | 1            |

    +----+-------+--------+--------------+

    编写一个 SQL 查询,找出每个部门工资前三高的员工。

    SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM (SELECT Name,Salary, DepartmentId,  RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS 'rank'  FROM Employee) AS e LEFT JOIN Department d ON e.DepartmentId=d.Id WHERE e.rank<=3

    项目十二 分数排名 - (难度:中等)

    依然是昨天的分数表,实现排名功能,但是排名是非连续的

    +-------+------+

    | Score | Rank |

    +-------+------+

    | 4.00  | 1    |

    | 4.00  | 1    |

    | 3.85  | 3    |

    | 3.65  | 4    |

    | 3.65  | 4    |

    | 3.50  | 6    |

    +-------+------

    在这里可以用Rank函数,也可以用上面的方式。

    SELECT Score, RANK() OVER (ORDER BY Score DESC) AS 'Rank' FROM score

    相关文章

      网友评论

          本文标题:MySQL 4

          本文链接:https://www.haomeiwen.com/subject/rxsciqtx.html