CS-Notes/notes/Leetcode-Database 题解.md

1099 lines
22 KiB
Java
Raw Normal View History

2019-04-25 18:24:51 +08:00
<!-- GFM-TOC -->
* [595. Big Countries](#595-big-countries)
* [627. Swap Salary](#627-swap-salary)
* [620. Not Boring Movies](#620-not-boring-movies)
* [596. Classes More Than 5 Students](#596-classes-more-than-5-students)
* [182. Duplicate Emails](#182-duplicate-emails)
* [196. Delete Duplicate Emails](#196-delete-duplicate-emails)
* [175. Combine Two Tables](#175-combine-two-tables)
* [181. Employees Earning More Than Their Managers](#181-employees-earning-more-than-their-managers)
* [183. Customers Who Never Order](#183-customers-who-never-order)
* [184. Department Highest Salary](#184-department-highest-salary)
* [176. Second Highest Salary](#176-second-highest-salary)
* [177. Nth Highest Salary](#177-nth-highest-salary)
* [178. Rank Scores](#178-rank-scores)
* [180. Consecutive Numbers](#180-consecutive-numbers)
* [626. Exchange Seats](#626-exchange-seats)
<!-- GFM-TOC -->
# 595. Big Countries
https://leetcode.com/problems/big-countries/description/
## Description
```html
+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+
```
查找面积超过 3,000,000 或者人口数超过 25,000,000 的国家
```html
+--------------+-------------+--------------+
| name | population | area |
+--------------+-------------+--------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+
```
2019-12-11 00:19:49 +08:00
## Solution
```sql
SELECT name,
population,
area
FROM
World
WHERE
area > 3000000
OR population > 25000000;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
2019-12-11 00:19:49 +08:00
SQL Schema 用于在本地环境下创建表结构并导入数据从而方便在本地环境调试
2019-06-09 17:04:10 +08:00
2019-04-25 18:24:51 +08:00
```sql
DROP TABLE
IF
EXISTS World;
CREATE TABLE World ( NAME VARCHAR ( 255 ), continent VARCHAR ( 255 ), area INT, population INT, gdp INT );
INSERT INTO World ( NAME, continent, area, population, gdp )
VALUES
( 'Afghanistan', 'Asia', '652230', '25500100', '203430000' ),
( 'Albania', 'Europe', '28748', '2831741', '129600000' ),
( 'Algeria', 'Africa', '2381741', '37100000', '1886810000' ),
( 'Andorra', 'Europe', '468', '78115', '37120000' ),
( 'Angola', 'Africa', '1246700', '20609294', '1009900000' );
```
# 627. Swap Salary
https://leetcode.com/problems/swap-salary/description/
## Description
```html
| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
```
只用一个 SQL 查询 sex 字段反转
```html
| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
```
2019-12-11 00:19:49 +08:00
## Solution
两个相等的数异或的结果为 0 0 与任何一个数异或的结果为这个数
sex 字段只有两个取值'f' 'm'并且有以下规律
```
'f' ^ ('m' ^ 'f') = 'm' ^ ('f' ^ 'f') = 'm'
'm' ^ ('m' ^ 'f') = 'f' ^ ('m' ^ 'm') = 'f'
```
因此将 sex 字段和 'm' ^ 'f' 进行异或操作最后就能反转 sex 字段
```sql
UPDATE salary
SET sex = CHAR ( ASCII(sex) ^ ASCII( 'm' ) ^ ASCII( 'f' ) );
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS salary;
CREATE TABLE salary ( id INT, NAME VARCHAR ( 100 ), sex CHAR ( 1 ), salary INT );
INSERT INTO salary ( id, NAME, sex, salary )
VALUES
( '1', 'A', 'm', '2500' ),
( '2', 'B', 'f', '1500' ),
( '3', 'C', 'm', '5500' ),
( '4', 'D', 'f', '500' );
```
# 620. Not Boring Movies
https://leetcode.com/problems/not-boring-movies/description/
## Description
```html
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card| Interesting| 9.1 |
+---------+-----------+--------------+-----------+
```
查找 id 为奇数并且 description 不是 boring 的电影 rating 降序
```html
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
+---------+-----------+--------------+-----------+
```
2019-12-11 00:19:49 +08:00
## Solution
```sql
SELECT
*
FROM
cinema
WHERE
id % 2 = 1
AND description != 'boring'
ORDER BY
rating DESC;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS cinema;
CREATE TABLE cinema ( id INT, movie VARCHAR ( 255 ), description VARCHAR ( 255 ), rating FLOAT ( 2, 1 ) );
INSERT INTO cinema ( id, movie, description, rating )
VALUES
( 1, 'War', 'great 3D', 8.9 ),
( 2, 'Science', 'fiction', 8.5 ),
( 3, 'irish', 'boring', 6.2 ),
( 4, 'Ice song', 'Fantacy', 8.6 ),
( 5, 'House card', 'Interesting', 9.1 );
```
# 596. Classes More Than 5 Students
https://leetcode.com/problems/classes-more-than-5-students/description/
## Description
```html
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+
```
查找有五名及以上 student class
```html
+---------+
| class |
+---------+
| Math |
+---------+
```
2019-12-11 00:19:49 +08:00
## Solution
2019-12-11 00:26:43 +08:00
class 列进行分组之后再使用 count 汇总函数统计每个分组的记录个数之后使用 HAVING 进行筛选HAVING 针对分组进行筛选 WHERE 针对每个记录进行筛选
2019-12-11 00:19:49 +08:00
```sql
SELECT
class
FROM
courses
GROUP BY
class
HAVING
count( DISTINCT student ) >= 5;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS courses;
CREATE TABLE courses ( student VARCHAR ( 255 ), class VARCHAR ( 255 ) );
INSERT INTO courses ( student, class )
VALUES
( 'A', 'Math' ),
( 'B', 'English' ),
( 'C', 'Math' ),
( 'D', 'Biology' ),
( 'E', 'Math' ),
( 'F', 'Computer' ),
( 'G', 'Math' ),
( 'H', 'Math' ),
( 'I', 'Math' );
```
# 182. Duplicate Emails
https://leetcode.com/problems/duplicate-emails/description/
## Description
邮件地址表
```html
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
```
查找重复的邮件地址
```html
+---------+
| Email |
+---------+
| a@b.com |
+---------+
```
## Solution
2019-12-11 00:26:43 +08:00
Email 进行分组如果并使用 COUNT 进行计数统计结果大于等于 2 的表示 Email 重复
2019-06-09 17:04:10 +08:00
2019-04-25 18:24:51 +08:00
```sql
SELECT
Email
FROM
Person
GROUP BY
Email
HAVING
COUNT( * ) >= 2;
```
2019-12-11 00:19:49 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( Id INT, Email VARCHAR ( 255 ) );
INSERT INTO Person ( Id, Email )
VALUES
( 1, 'a@b.com' ),
( 2, 'c@d.com' ),
( 3, 'a@b.com' );
```
2019-04-25 18:24:51 +08:00
# 196. Delete Duplicate Emails
https://leetcode.com/problems/delete-duplicate-emails/description/
## Description
邮件地址表
```html
+----+---------+
| Id | Email |
+----+---------+
2019-06-09 17:04:10 +08:00
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
2019-04-25 18:24:51 +08:00
+----+---------+
```
删除重复的邮件地址
```html
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
```
## Solution
2019-06-09 17:04:10 +08:00
只保留相同 Email Id 最小的那一个然后删除其它的
2019-12-11 00:26:43 +08:00
连接查询
2019-04-25 18:24:51 +08:00
```sql
DELETE p1
FROM
Person p1,
Person p2
WHERE
p1.Email = p2.Email
AND p1.Id > p2.Id
```
子查询
```sql
DELETE
FROM
Person
WHERE
2019-12-11 00:26:43 +08:00
id NOT IN (
SELECT id
FROM (
SELECT min( id ) AS id
FROM Person
GROUP BY email
) AS m
);
2019-04-25 18:24:51 +08:00
```
应该注意的是上述解法额外嵌套了一个 SELECT 语句如果不这么做会出现错误You can't specify target table 'Person' for update in FROM clause以下演示了这种错误解法
```sql
DELETE
FROM
Person
WHERE
2019-12-11 00:26:43 +08:00
id NOT IN (
SELECT min( id ) AS id
FROM Person
GROUP BY email
);
2019-04-25 18:24:51 +08:00
```
参考[pMySQL Error 1093 - Can't specify target table for update in FROM clause](https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause)
2019-12-11 00:19:49 +08:00
## SQL Schema
182 相同
2019-04-25 18:24:51 +08:00
# 175. Combine Two Tables
https://leetcode.com/problems/combine-two-tables/description/
## Description
Person
```html
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
```
Address
```html
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
```
查找 FirstName, LastName, City, State 数据而不管一个用户有没有填地址信息
2019-12-11 00:19:49 +08:00
## Solution
涉及到 Person Address 两个表在对这两个表执行连接操作时因为要保留 Person 表中的信息即使在 Address 表中没有关联的信息也要保留此时可以用左外连接 Person 表放在 LEFT JOIN 的左边
```sql
SELECT
FirstName,
LastName,
City,
State
FROM
Person P
LEFT JOIN Address A
ON P.PersonId = A.PersonId;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Person;
CREATE TABLE Person ( PersonId INT, FirstName VARCHAR ( 255 ), LastName VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Address;
CREATE TABLE Address ( AddressId INT, PersonId INT, City VARCHAR ( 255 ), State VARCHAR ( 255 ) );
INSERT INTO Person ( PersonId, LastName, FirstName )
VALUES
( 1, 'Wang', 'Allen' );
INSERT INTO Address ( AddressId, PersonId, City, State )
VALUES
( 1, 2, 'New York City', 'New York' );
```
# 181. Employees Earning More Than Their Managers
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/
## Description
Employee
```html
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
```
查找薪资大于其经理薪资的员工信息
2019-12-11 00:19:49 +08:00
## Solution
```sql
SELECT
E1.NAME AS Employee
FROM
Employee E1
INNER JOIN Employee E2
ON E1.ManagerId = E2.Id
AND E1.Salary > E2.Salary;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, ManagerId INT );
INSERT INTO Employee ( Id, NAME, Salary, ManagerId )
VALUES
( 1, 'Joe', 70000, 3 ),
( 2, 'Henry', 80000, 4 ),
( 3, 'Sam', 60000, NULL ),
( 4, 'Max', 90000, NULL );
```
# 183. Customers Who Never Order
https://leetcode.com/problems/customers-who-never-order/description/
## Description
Customers
```html
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
```
Orders
```html
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
```
查找没有订单的顾客信息
```html
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
```
## Solution
左外链接
```sql
SELECT
C.Name AS Customers
FROM
Customers C
LEFT JOIN Orders O
ON C.Id = O.CustomerId
WHERE
O.CustomerId IS NULL;
```
子查询
```sql
SELECT
Name AS Customers
FROM
Customers
WHERE
2019-12-11 01:27:45 +08:00
Id NOT IN (
SELECT CustomerId
FROM Orders
);
2019-04-25 18:24:51 +08:00
```
2019-12-11 00:19:49 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Customers;
CREATE TABLE Customers ( Id INT, NAME VARCHAR ( 255 ) );
DROP TABLE
IF
EXISTS Orders;
CREATE TABLE Orders ( Id INT, CustomerId INT );
INSERT INTO Customers ( Id, NAME )
VALUES
( 1, 'Joe' ),
( 2, 'Henry' ),
( 3, 'Sam' ),
( 4, 'Max' );
INSERT INTO Orders ( Id, CustomerId )
VALUES
( 1, 3 ),
( 2, 1 );
```
2019-04-25 18:24:51 +08:00
# 184. Department Highest Salary
https://leetcode.com/problems/department-highest-salary/description/
## Description
Employee
```html
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
```
Department
```html
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
```
查找一个 Department 中收入最高者的信息
```html
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
```
## Solution
创建一个临时表包含了部门员工的最大薪资可以对部门进行分组然后使用 MAX() 汇总函数取得最大薪资
之后使用连接找到一个部门中薪资等于临时表中最大薪资的员工
```sql
SELECT
D.NAME Department,
E.NAME Employee,
E.Salary
FROM
Employee E,
Department D,
2019-12-11 01:27:45 +08:00
( SELECT DepartmentId, MAX( Salary ) Salary
FROM Employee
GROUP BY DepartmentId ) M
2019-04-25 18:24:51 +08:00
WHERE
E.DepartmentId = D.Id
AND E.DepartmentId = M.DepartmentId
AND E.Salary = M.Salary;
```
2019-12-11 00:19:49 +08:00
## SQL Schema
```sql
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee ( Id INT, NAME VARCHAR ( 255 ), Salary INT, DepartmentId INT );
DROP TABLE IF EXISTS Department;
CREATE TABLE Department ( Id INT, NAME VARCHAR ( 255 ) );
INSERT INTO Employee ( Id, NAME, Salary, DepartmentId )
VALUES
( 1, 'Joe', 70000, 1 ),
( 2, 'Henry', 80000, 2 ),
( 3, 'Sam', 60000, 2 ),
( 4, 'Max', 90000, 1 );
INSERT INTO Department ( Id, NAME )
VALUES
( 1, 'IT' ),
( 2, 'Sales' );
```
2019-04-25 18:24:51 +08:00
# 176. Second Highest Salary
https://leetcode.com/problems/second-highest-salary/description/
## Description
```html
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
```
查找工资第二高的员工
```html
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
```
没有找到返回 null 而不是不返回数据
2019-12-11 00:19:49 +08:00
## Solution
为了在没有查找到数据时返回 null需要在查询结果外面再套一层 SELECT
```sql
SELECT
2019-12-11 01:27:45 +08:00
( SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1, 1 ) SecondHighestSalary;
2019-12-11 00:19:49 +08:00
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Employee;
CREATE TABLE Employee ( Id INT, Salary INT );
INSERT INTO Employee ( Id, Salary )
VALUES
( 1, 100 ),
( 2, 200 ),
( 3, 300 );
```
# 177. Nth Highest Salary
## Description
查找工资第 N 高的员工
## Solution
```sql
CREATE FUNCTION getNthHighestSalary ( N INT ) RETURNS INT BEGIN
SET N = N - 1;
2019-12-11 01:27:45 +08:00
RETURN (
SELECT (
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT N, 1
)
);
2019-04-25 18:24:51 +08:00
END
```
2019-12-11 00:19:49 +08:00
## SQL Schema
176
2019-04-25 18:24:51 +08:00
# 178. Rank Scores
https://leetcode.com/problems/rank-scores/description/
## Description
得分表
```html
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
```
将得分排序并统计排名
```html
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
```
## Solution
2019-12-11 01:27:45 +08:00
要统计某个 score 的排名只要统计大于等于该 score score 数量
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
| Id | score | 大于等于该 score score 数量 | 排名 |
| :---: | :---: | :---: | :---: |
| 1 | 4.1 | 3 | 3 |
| 2 | 4.2 | 2 | 2 |
| 3 | 4.3 | 1 | 1 |
2019-06-09 17:04:10 +08:00
2020-05-27 01:18:59 +08:00
使用连接操作找到某个 score 对应的大于等于其值的记录
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
```sql
SELECT
*
FROM
Scores S1
INNER JOIN Scores S2
ON S1.score <= S2.score
ORDER BY
S1.score DESC, S1.Id;
```
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
| S1.Id | S1.score | S2.Id | S2.score |
| :---: | :---: | :---: | :---: |
|3| 4.3| 3 |4.3|
|2| 4.2| 2| 4.2|
|2| 4.2 |3 |4.3|
|1| 4.1 |1| 4.1|
|1| 4.1 |2| 4.2|
|1| 4.1 |3| 4.3|
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
可以看到每个 S1.score 都有对应好几条记录我们再进行分组并统计每个分组的数量作为 'Rank'
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
```sql
SELECT
S1.score 'Score',
COUNT(*) 'Rank'
FROM
Scores S1
INNER JOIN Scores S2
ON S1.score <= S2.score
GROUP BY
S1.id, S1.score
ORDER BY
S1.score DESC, S1.Id;
```
| score | Rank |
2019-06-09 17:04:10 +08:00
| :---: | :---: |
| 4.3 | 1 |
2019-12-11 01:27:45 +08:00
| 4.2 | 2 |
| 4.1 | 3 |
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
上面的解法看似没问题但是对于以下数据它却得到了错误的结果
2019-06-09 17:04:10 +08:00
2019-12-11 01:27:45 +08:00
| Id | score |
| :---: | :---: |
| 1 | 4.1 |
| 2 | 4.2 |
| 3 | 4.2 |
| score | Rank |
| :---: | :--: |
| 4.2 | 2 |
| 4.2 | 2 |
| 4.1 | 3 |
而我们希望的结果为
| score | Rank |
| :---: | :--: |
| 4.2 | 1 |
2020-05-27 01:18:59 +08:00
| 4.2 | 1 |
2019-12-11 01:27:45 +08:00
| 4.1 | 2 |
连接情况如下
| S1.Id | S1.score | S2.Id | S2.score |
| :---: | :------: | :---: | :------: |
| 2 | 4.2 | 3 | 4.2 |
| 2 | 4.2 | 2 | 4.2 |
| 3 | 4.2 | 3 | 4.2 |
| 3 | 4.2 | 2 | 4.1 |
| 1 | 4.1 | 3 | 4.2 |
| 1 | 4.1 | 2 | 4.3 |
| 1 | 4.1 | 1 | 4.1 |
我们想要的结果是把分数相同的放在同一个排名并且相同分数只占一个位置例如上面的分数Id=2 Id=3 的记录都有相同的分数并且最高他们并列第一 Id=1 的记录应该排第二名而不是第三名所以在进行 COUNT 计数统计时我们需要使用 COUNT( DISTINCT S2.score ) 从而只统计一次相同的分数
2019-06-09 17:04:10 +08:00
2019-04-25 18:24:51 +08:00
```sql
SELECT
2019-06-09 17:04:10 +08:00
S1.score 'Score',
COUNT( DISTINCT S2.score ) 'Rank'
2019-04-25 18:24:51 +08:00
FROM
Scores S1
INNER JOIN Scores S2
ON S1.score <= S2.score
GROUP BY
2019-04-26 21:18:14 +08:00
S1.id, S1.score
2019-04-25 18:24:51 +08:00
ORDER BY
S1.score DESC;
```
2019-12-11 00:19:49 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS Scores;
CREATE TABLE Scores ( Id INT, Score DECIMAL ( 3, 2 ) );
INSERT INTO Scores ( Id, Score )
VALUES
2019-12-11 01:27:45 +08:00
( 1, 4.1 ),
( 2, 4.1 ),
( 3, 4.2 ),
( 4, 4.2 ),
( 5, 4.3 ),
( 6, 4.3 );
2019-12-11 00:19:49 +08:00
```
2019-04-25 18:24:51 +08:00
# 180. Consecutive Numbers
https://leetcode.com/problems/consecutive-numbers/description/
## Description
数字表
```html
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
```
查找连续出现三次的数字
```html
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
```
2019-12-11 00:19:49 +08:00
## Solution
```sql
SELECT
DISTINCT L1.num ConsecutiveNums
FROM
Logs L1,
Logs L2,
Logs L3
WHERE L1.id = l2.id - 1
AND L2.id = L3.id - 1
AND L1.num = L2.num
AND l2.num = l3.num;
```
2019-04-25 18:24:51 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS LOGS;
CREATE TABLE LOGS ( Id INT, Num INT );
INSERT INTO LOGS ( Id, Num )
VALUES
( 1, 1 ),
( 2, 1 ),
( 3, 1 ),
( 4, 2 ),
( 5, 1 ),
( 6, 2 ),
( 7, 2 );
```
# 626. Exchange Seats
https://leetcode.com/problems/exchange-seats/description/
## Description
seat 表存储着座位对应的学生
```html
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
```
要求交换相邻座位的两个学生如果最后一个座位是奇数那么不交换这个座位上的学生
```html
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
```
## Solution
使用多个 union
```sql
2019-06-09 17:04:10 +08:00
# 处理偶数 id id 1
# 例如 2,4,6,... 变成 1,3,5,...
2019-04-25 18:24:51 +08:00
SELECT
s1.id - 1 AS id,
s1.student
FROM
seat s1
WHERE
s1.id MOD 2 = 0 UNION
2019-06-09 17:04:10 +08:00
# 处理奇数 id id 1但是如果最大的 id 为奇数则不做处理
# 例如 1,3,5,... 变成 2,4,6,...
2019-04-25 18:24:51 +08:00
SELECT
s2.id + 1 AS id,
s2.student
FROM
seat s2
WHERE
s2.id MOD 2 = 1
AND s2.id != ( SELECT max( s3.id ) FROM seat s3 ) UNION
2019-06-09 17:04:10 +08:00
# 如果最大的 id 为奇数单独取出这个数
2019-04-25 18:24:51 +08:00
SELECT
s4.id AS id,
s4.student
FROM
seat s4
WHERE
s4.id MOD 2 = 1
AND s4.id = ( SELECT max( s5.id ) FROM seat s5 )
ORDER BY
id;
```
2019-12-11 00:19:49 +08:00
## SQL Schema
```sql
DROP TABLE
IF
EXISTS seat;
CREATE TABLE seat ( id INT, student VARCHAR ( 255 ) );
INSERT INTO seat ( id, student )
VALUES
( '1', 'Abbot' ),
( '2', 'Doris' ),
( '3', 'Emerson' ),
( '4', 'Green' ),
( '5', 'Jeames' );
```
2019-04-25 18:24:51 +08:00
2019-10-28 00:25:00 +08:00
2019-11-02 17:33:10 +08:00
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>