Skip to content

Commit 612d266

Browse files
committed
auto commit
1 parent 3b3fac2 commit 612d266

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

docs/notes/Leetcode-Database 题解.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ https://leetcode.com/problems/classes-more-than-5-students/description/
230230

231231
## Solution
232232

233-
对 class 列进行分组之后,再使用 count 汇总函数统计数量,统计之后使用 having 进行过滤
233+
对 class 列进行分组之后,再使用 count 汇总函数统计每个分组的记录个数,之后使用 HAVING 进行筛选。HAVING 针对分组进行筛选,而 WHERE 针对每个记录(行)进行筛选
234234

235235
```sql
236236
SELECT
@@ -293,7 +293,7 @@ https://leetcode.com/problems/duplicate-emails/description/
293293

294294
## Solution
295295

296-
对 Email 进行分组,如果相同 Email 的数量大于等于 2,则表示该 Email 重复。
296+
对 Email 进行分组,如果并使用 COUNT 进行计数统计,结果大于等于 2 的表示 Email 重复。
297297

298298
```sql
299299
SELECT
@@ -354,7 +354,7 @@ https://leetcode.com/problems/delete-duplicate-emails/description/
354354

355355
只保留相同 Email 中 Id 最小的那一个,然后删除其它的。
356356

357-
连接
357+
连接查询
358358

359359
```sql
360360
DELETE p1
@@ -373,7 +373,14 @@ DELETE
373373
FROM
374374
Person
375375
WHERE
376-
id NOT IN ( SELECT id FROM ( SELECT min( id ) AS id FROM Person GROUP BY email ) AS m );
376+
id NOT IN (
377+
SELECT id
378+
FROM (
379+
SELECT min( id ) AS id
380+
FROM Person
381+
GROUP BY email
382+
) AS m
383+
);
377384
```
378385

379386
应该注意的是上述解法额外嵌套了一个 SELECT 语句,如果不这么做,会出现错误:You can't specify target table 'Person' for update in FROM clause。以下演示了这种错误解法。
@@ -383,7 +390,11 @@ DELETE
383390
FROM
384391
Person
385392
WHERE
386-
id NOT IN ( SELECT min( id ) AS id FROM Person GROUP BY email );
393+
id NOT IN (
394+
SELECT min( id ) AS id
395+
FROM Person
396+
GROUP BY email
397+
);
387398
```
388399

389400
参考:[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)
@@ -392,8 +403,6 @@ WHERE
392403

393404
与 182 相同。
394405

395-
396-
397406
# 175. Combine Two Tables
398407

399408
https://leetcode.com/problems/combine-two-tables/description/

notes/Leetcode-Database 题解.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ https://leetcode.com/problems/classes-more-than-5-students/description/
230230

231231
## Solution
232232

233-
对 class 列进行分组之后,再使用 count 汇总函数统计数量,统计之后使用 having 进行过滤
233+
对 class 列进行分组之后,再使用 count 汇总函数统计每个分组的记录个数,之后使用 HAVING 进行筛选。HAVING 针对分组进行筛选,而 WHERE 针对每个记录(行)进行筛选
234234

235235
```sql
236236
SELECT
@@ -293,7 +293,7 @@ https://leetcode.com/problems/duplicate-emails/description/
293293

294294
## Solution
295295

296-
对 Email 进行分组,如果相同 Email 的数量大于等于 2,则表示该 Email 重复。
296+
对 Email 进行分组,如果并使用 COUNT 进行计数统计,结果大于等于 2 的表示 Email 重复。
297297

298298
```sql
299299
SELECT
@@ -354,7 +354,7 @@ https://leetcode.com/problems/delete-duplicate-emails/description/
354354

355355
只保留相同 Email 中 Id 最小的那一个,然后删除其它的。
356356

357-
连接
357+
连接查询
358358

359359
```sql
360360
DELETE p1
@@ -373,7 +373,14 @@ DELETE
373373
FROM
374374
Person
375375
WHERE
376-
id NOT IN ( SELECT id FROM ( SELECT min( id ) AS id FROM Person GROUP BY email ) AS m );
376+
id NOT IN (
377+
SELECT id
378+
FROM (
379+
SELECT min( id ) AS id
380+
FROM Person
381+
GROUP BY email
382+
) AS m
383+
);
377384
```
378385

379386
应该注意的是上述解法额外嵌套了一个 SELECT 语句,如果不这么做,会出现错误:You can't specify target table 'Person' for update in FROM clause。以下演示了这种错误解法。
@@ -383,7 +390,11 @@ DELETE
383390
FROM
384391
Person
385392
WHERE
386-
id NOT IN ( SELECT min( id ) AS id FROM Person GROUP BY email );
393+
id NOT IN (
394+
SELECT min( id ) AS id
395+
FROM Person
396+
GROUP BY email
397+
);
387398
```
388399

389400
参考:[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)
@@ -392,8 +403,6 @@ WHERE
392403

393404
与 182 相同。
394405

395-
396-
397406
# 175. Combine Two Tables
398407

399408
https://leetcode.com/problems/combine-two-tables/description/

0 commit comments

Comments
 (0)