【数据库】MySQL 实战 - 复杂项目

4.2 MySQL 实战 - 复杂项目

#作业#

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

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

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

| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|

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

| 1 | 1 | 10 | 1 | completed |2013-10-01|

| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|

| 3 | 3 | 12 | 6 | completed |2013-10-01|

| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|

| 5 | 1 | 10 | 1 | completed |2013-10-02|

| 6 | 2 | 11 | 6 | completed |2013-10-02|

| 7 | 3 | 12 | 6 | completed |2013-10-02|

| 8 | 2 | 12 | 12 | completed |2013-10-03|

| 9 | 3 | 10 | 12 | completed |2013-10-03|

| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|

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

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

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

| Users_Id | Banned | Role |

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

| 1 | No | client |

| 2 | Yes | client |

| 3 | No | client |

| 4 | No | client |

| 10 | No | driver |

| 11 | No | driver |

| 12 | No | driver |

| 13 | No | driver |

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

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

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

| Day | Cancellation Rate |

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

| 2013-10-01 | 0.33 |

| 2013-10-02 | 0.00 |

| 2013-10-03 | 0.50 |

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

 

 

项目十一:各部门前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 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

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

| Department | Employee | Salary |

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

| IT | Max | 90000 |

| IT | Randy | 85000 |

| IT | Joe | 70000 |

| Sales | Henry | 80000 |

| Sales | Sam | 60000 |

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

 

此外,请考虑实现各部门前N高工资的员工功能。

 

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

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

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

| Score | Rank |

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

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 3 |

| 3.65 | 4 |

| 3.65 | 4 |

| 3.50 | 6 |

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

4.2 MySQL 实战 - 复杂项目

项目十:行程和用户(难度:困难)(还没写出来,参考了别人的)

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

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

| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|

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

| 1 | 1 | 10 | 1 | completed |2013-10-01|

| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|

| 3 | 3 | 12 | 6 | completed |2013-10-01|

| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|

| 5 | 1 | 10 | 1 | completed |2013-10-02|

| 6 | 2 | 11 | 6 | completed |2013-10-02|

| 7 | 3 | 12 | 6 | completed |2013-10-02|

| 8 | 2 | 12 | 12 | completed |2013-10-03|

| 9 | 3 | 10 | 12 | completed |2013-10-03|

| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|

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

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

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

| Users_Id | Banned | Role |

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

| 1 | No | client |

| 2 | Yes | client |

| 3 | No | client |

| 4 | No | client |

| 10 | No | driver |

| 11 | No | driver |

| 12 | No | driver |

| 13 | No | driver |

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

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

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

| Day | Cancellation Rate |

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

| 2013-10-01 | 0.33 |

| 2013-10-02 | 0.00 |

| 2013-10-03 | 0.50 |

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

解答:

1、创建Users表,并为它插入数据:

CREATE TABLE Users (
    Users_Id INT NOT NULL PRIMARY KEY,
    Banned varchar(3) NOT NULL,
    Role ENUM('client', 'driver','partner')
);

insert into Users(Users_Id,Banned,Role) values(1,'No','client');
insert into Users(Users_Id,Banned,Role) values(2,'Yes','client');
insert into Users(Users_Id,Banned,Role) values(3,'No','client');
insert into Users(Users_Id,Banned,Role) values(4,'No','client');
insert into Users(Users_Id,Banned,Role) values(10,'No','driver');
insert into Users(Users_Id,Banned,Role) values(11,'No','driver');
insert into Users(Users_Id,Banned,Role) values(12,'No','driver');
insert into Users(Users_Id,Banned,Role) values(13,'No','driver');

【数据库】MySQL 实战 - 复杂项目_第1张图片

2、创建Trip表,并为它插入数据:

CREATE TABLE Trips (
    Id INT NOT NULL 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_at DATE,
    FOREIGN KEY(Client_Id) REFERENCES Users(Users_Id),
    FOREIGN KEY(Driver_Id) REFERENCES Users(Users_Id)
);

INSERT INTO Trips VALUES(1,1,10,1,'completed','2013-10-01');
INSERT INTO Trips VALUES(2,2,11,1,'cancelled_by_driver','2013-10-01');
INSERT INTO Trips VALUES(3,3,12,6,'completed','2013-10-01');
INSERT INTO Trips VALUES(4,4,13,6,'cancelled_by_client','2013-10-01');
INSERT INTO Trips VALUES(5,1,10,1,'completed','2013-10-02');
INSERT INTO Trips VALUES(6,2,11,6,'completed','2013-10-02');
INSERT INTO Trips VALUES(7,3,12,6,'completed','2013-10-02');
INSERT INTO Trips VALUES(8,2,12,12,'completed','2013-10-03');
INSERT INTO Trips VALUES(9,3,10,12,'completed','2013-10-03');
INSERT INTO Trips VALUES(10,4,13,12,'cancelled_by_driver','2013-10-03');

【数据库】MySQL 实战 - 复杂项目_第2张图片

3、查询;

SELECT T2.DAY,IFNULL(ROUND((T1.num/T2.num),2),0) AS 'Cancellation Rate'
FROM
(SELECT Request_at AS DAY,COUNT(*) AS num
	FROM Trips t
	LEFT JOIN Users u
	ON t.Client_Id = u.Users_Id
  WHERE u.Banned != 'Yes'
  AND t.status != 'completed'
  AND Request_at >='2013-10-01' AND Request_at <= '2013-10-03'
	GROUP BY DAY) AS T1
RIGHT JOIN
 (SELECT Request_at AS DAY,COUNT(*) AS num
	FROM Trips t
	LEFT JOIN Users u
	ON t.Client_Id = u.Users_Id
  WHERE u.Banned != 'Yes'
  AND Request_at >='2013-10-01' AND Request_at <= '2013-10-03'
	GROUP BY DAY) AS T2
  ON T1.DAY = T2.DAY;

【数据库】MySQL 实战 - 复杂项目_第3张图片

项目十一:各部门前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 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

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

| Department | Employee | Salary |

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

| IT | Max | 90000 |

| IT | Randy | 85000 |

| IT | Joe | 70000 |

| Sales | Henry | 80000 |

| Sales | Sam | 60000 |

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

此外,请考虑实现各部门前N高工资的员工功能。

解答:

1、删除Employee表:

【数据库】MySQL 实战 - 复杂项目_第4张图片

2、创建新的Employee表:

CREATE TABLE Employee (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(255),
salary INT,
departmentid INT
);

【数据库】MySQL 实战 - 复杂项目_第5张图片

2、插入数据:

INSERT INTO Employee VALUES (1,'Joe',70000,1);
INSERT INTO Employee VALUES (2,'Henry',80000,2);
INSERT INTO Employee VALUES (3,'Sam',60000,2);
INSERT INTO Employee VALUES (4,'Max',90000,1);
INSERT INTO Employee VALUES (5,'Janet',69000,1);
INSERT INTO Employee VALUES (6,'Randy',85000,1);

【数据库】MySQL 实战 - 复杂项目_第6张图片

3、查询数据:

SELECT
	d. NAME AS Department,
	e. NAME AS Employee,
	e.salary AS Salary
FROM employee AS e
INNER JOIN department AS d ON e.DepartmentId = d.id
WHERE (
  SELECT COUNT(DISTINCT salary)
	FROM employee
	WHERE salary > e.salary
	AND departmentid = e.DepartmentId
	) < 3
ORDER BY e.departmentid,Salary DESC

【数据库】MySQL 实战 - 复杂项目_第7张图片

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

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

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

| Score | Rank |

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

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 3 |

| 3.65 | 4 |

| 3.65 | 4 |

| 3.50 | 6 |

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

 解答:

1、创建scores表:

CREATE TABLE scores (
    Id INT NOT NULL PRIMARY KEY,
    Score FLOAT(5,2) NOT NULL
);

【数据库】MySQL 实战 - 复杂项目_第8张图片

2、插入数据:

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

【数据库】MySQL 实战 - 复杂项目_第9张图片

3、查询数据:

SELECT 
    s.Score,
    (SELECT 
            COUNT(*) + 1
        FROM
            Scores AS s1
        WHERE
            s1.Score > s.Score) AS Rank
FROM
    scores s
ORDER BY Score DESC;

【数据库】MySQL 实战 - 复杂项目_第10张图片

你可能感兴趣的:(数据库)