1. 程式人生 > >mysql 兩個表合並查詢 字段交換

mysql 兩個表合並查詢 字段交換

count 示例 3.5 給定 led day nbsp 枚舉類 jea

項目七: 各部門工資最高的員工(難度:中等)

創建 Employee 表,包含所有員工信息,每個員工有其對應的 Id, salary 和 department Id。

create table Employee(

Id int auto_increment primary key,

Name varchar not null,

Salary int not null,

DepartmentId int not null

);

insert into Emploee (Name,Salary,DepartmentId) values(Joe,70000,1);

insert into Emploee(Name,Salary,DepartmentId) values(Henry,80000,2);

insert into Emploee(Name,Salary,DepartmentId) values(Sam,60000,2);

insert into Emploee(Name,Salary,DepartmentId) values(Max,90000,1);

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

| Id | Name | Salary | DepartmentId |

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

| 1 | Joe | 70000 | 1 |

| 2 | Henry | 80000 | 2 |

| 3 | Sam | 60000 | 2 |

| 4 | Max | 90000 | 1 |

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

創建 Department 表,包含公司所有部門的信息。

create table Department(

Id int primary key auto_increment,

Name varchar not null

);

insert into Department(Name) values (IT);

insert into Department(Name) values (Sales);

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

| Id | Name |

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

| 1 | IT |

| 2 | Sales |

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

編寫一個 SQL 查詢,找出每個部門工資最高的員工。例如,根據上述給定的表格,Max 在 IT 部門有最高工資,Henry 在 Sales 部門有最高工資。

select Department.Name as Department,Emploe.Name as Emploee,max(Emploee.Salary) as Salary from Emploee,Department where Employee.Id=Department.Id group by Emploee.DepartmentId;

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

| Department | Employee | Salary |

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

| IT | Max | 90000 |

| Sales | Henry | 80000 |

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

項目八: 換座位(難度:中等)

小是一所中學的信息科技老師,她有一張 seat 座位表,平時用來儲存學生名字和與他們相對應的座位 id。

其中縱列的 id 是連續遞增的

小美想改變相鄰倆學生的座位。

你能不能幫她寫一個 SQL query 來輸出小美想要的結果呢?

請創建如下所示 seat 表:

示例:

create table seat(

id int primary key auto_increment,

student varchar not null

);

insert into seat(student) values(Abbot);

insert into seat(student) values(Doris);

insert into seat(student) values(Emerson);

insert into seat(student) values(Green);

insert into seat(student) values(Jeames);

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

| id | student |

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

| 1 | Abbot |

| 2 | Doris |

| 3 | Emerson |

| 4 | Green |

| 5 | Jeames |

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

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

UPDATE seat s1
JOIN seat s2
ON (s1.id % 2 = 1 AND s2.id = s1.id+1)
SET s1.student=s2.student,s2.student=s1.student

WHERE s1.id+1 <> null;

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

| id | student |

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

| 1 | Doris |

| 2 | Abbot |

| 3 | Green |

| 4 | Emerson |

| 5 | Jeames |

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

註意:

如果學生人數是奇數,則不需要改變最後一個同學的座位

項目九: 分數排名(難度:中等)

編寫一個 SQL 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(Rank)相同。請註意,平分後的下一個名次應該是下一個連續的整數值。換句話說,名次之間不應該有“間隔”。

創建以下 score 表:

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

| Id | Score |

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

| 1 | 3.50 |

| 2 | 3.65 |

| 3 | 4.00 |

| 4 | 3.85 |

| 5 | 4.00 |

| 6 | 3.65 |

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

create table score(

Id int auto_increment primary key,

Score float not null

);

例如,根據上述給定的 scores 表,你的查詢應該返回(按分數從高到低排列):

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

| Score | Rank |

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

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 2 |

| 3.65 | 3 |

| 3.65 | 3 |

| 3.50 | 4 |

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

SELECT Score,
CASE
WHEN @prevRank = Score THEN @curRank
WHEN @prevRank := Score THEN @curRank := @curRank + 1
END AS Rank
FROM scores,
(SELECT @curRank :=0, @prevRank := NULL)
ORDER BY Score desc;

項目十:行程和用戶(難度:困難)

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 |

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

select t1.Request_at,t1.c1/t2.c2 as Cancellation Rate from

(select count(Status) as c1 Request_at from Trips where Client_Id in (select Users_id from Users where Role=client && Banned=No;) and Status <> completed group by Request_at;) t1,

(select count(Status) as c2 Request_at from Trips where Client_Id in (select Users_id from Users where Role=client && Banned=No;) group by Request_at;) t2

where

t1.Request_at=t2.Request_at;

項目十一:各部門前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高工資的員工功能。

select Department.Name as Department,EmployeeName as Employee,Employee.Salary from

Department join Employee on

order by Employee.salary

desc

limit 3

group by

Department.name

where

Employee.Department_Id=Department.Id;

項目十二 分數排名 - (難度:中等)

依然是昨天的分數表,實現排名功能,但是排名是非連續的,如下:

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

| Score | Rank |

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

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 3 |

| 3.65 | 4 |

| 3.65 | 4 |

| 3.50 | 6 |

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

SELECT Score Rank FROM
(SELECT Score,
@curRank := IF(@prevRank = Score, @curRank, @incRank) AS Rank,
@incRank := @incRank + 1,
@prevRank := Score
FROM scores,(
SELECT @curRank :=0, @prevRank := NULL, @incRank := 1
)
ORDER BY Score desc;

mysql 兩個表合並查詢 字段交換