@@ -134,6 +134,7 @@ SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) >= 2;
134
134
135
135
``` mysql
136
136
# Write your MySQL query statement below
137
+
137
138
```
138
139
139
140
@@ -177,7 +178,7 @@ SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) >= 2;
177
178
178
179
``` mysql
179
180
# Write your MySQL query statement below
180
- SELECT * FROM cinema WHERE description != ' boring' AND id% 2 = 1
181
+ SELECT * FROM cinema WHERE description != ' boring' AND id % 2 = 1
181
182
ORDER BY rating DESC ;
182
183
```
183
184
@@ -224,6 +225,8 @@ FirstName, LastName, City, State
224
225
225
226
### 解答
226
227
228
+ 考察左连接 ` LEFT JOIN ` 的用法
229
+
227
230
``` mysql
228
231
# Write your MySQL query statement below
229
232
SELECT FirstName,LastName,City,State FROM Person LEFT JOIN Address ON Person .PersonId = Address .PersonId ;
@@ -260,13 +263,19 @@ SELECT FirstName,LastName,City,State FROM Person LEFT JOIN Address ON Person.Per
260
263
261
264
### 解答
262
265
266
+ #### 方法1
267
+
268
+ 对同一个表进行** 连接** ,过滤条件是 ` e1.ManagerId = e2.Id AND e1.Salary > e2.Salary ` 。
269
+
263
270
``` mysql
264
271
# Write your MySQL query statement below
265
272
SELECT e1 .name AS Employee FROM Employee AS e1,Employee AS e2
266
273
WHERE e1 .ManagerId = e2 .Id AND e1 .Salary > e2 .Salary ;
267
274
```
268
275
276
+ #### 方法2
269
277
278
+ 思路同** 方法1** ,连接方式变为` join...on... ` 。
270
279
271
280
``` mysql
272
281
# Write your MySQL query statement below
@@ -321,6 +330,8 @@ on a.managerid=b.id and a.salary>=b.salary;
321
330
322
331
#### 方法1
323
332
333
+ 利用子查询过滤,找出表` Customers ` 中的` Id ` 不在表` Orders ` 的` CustomersId ` 中的那些行。
334
+
324
335
``` mysql
325
336
# Write your MySQL query statement below
326
337
# select a.name as Customers from customers a join orders b on a.id = b.customerid;
@@ -331,6 +342,8 @@ where id not in
331
342
332
343
#### 方法2
333
344
345
+ 对` Customers ` 和` Orders ` 两个表进行左连接,连接条件是` Customers.Id = Orders.CustomersId ` ,那么连接之后` Orders ` 表中` Id ` 列为空的那些行即为所求。
346
+
334
347
``` mysql
335
348
# Write your MySQL query statement below
336
349
select c .name as Customers from customers as c left join orders as o
@@ -370,7 +383,9 @@ Id 是这个表的主键。
370
383
371
384
### 解答
372
385
373
- 由于 * MYSQL* 不能先` select ` 一个表的记录,然后在按此条件进行更新和删除同一个表的记录删除数据。因此创建临时表进行过渡
386
+ 对于重复的邮箱,保留` Id ` 最小的,相当于是以` Email ` 作为分组,保留每一组` Id ` 最小的那些行。
387
+
388
+ ** 注意** :由于 * MYSQL* 不能先` select ` 一个表的记录,然后在按此条件进行更新和删除同一个表的记录删除数据。因此创建临时表进行过渡。
374
389
375
390
``` mysql
376
391
# Write your MySQL query statement below
@@ -410,13 +425,21 @@ delete from person where id not in
410
425
411
426
### 解答
412
427
428
+ 对同一个表` Weather ` 创建两个临时表` a ` 和` b ` 进行连接,连接条件是:` a ` 的` recorddate ` 比` b ` 的` recorddate ` 大1并且` a ` 的` temperature ` 大于` b ` 的` temperature ` 。
429
+
430
+ #### 方法1
431
+
432
+ 利用` datediff ` 函数
433
+
413
434
``` mysql
414
435
# Write your MySQL query statement below
415
436
select a .id from weather as a join weather as b
416
437
on datediff(a .recorddate ,b .recorddate ) = 1 and a .temperature > b .temperature ;
417
438
```
418
439
440
+ #### 方法2
419
441
442
+ 利用` subdate ` 函数。
420
443
421
444
``` mysql
422
445
# Write your MySQL query statement below
@@ -467,6 +490,10 @@ on (a.temperature > b.temperature and subdate(a.RecordDate,1) = b.RecordDate);
467
490
468
491
### 解答
469
492
493
+ 以课程` class ` 进行分组,过滤出学生数不小于5的即可。
494
+
495
+ ** 注意** :加上` distinct ` 关键字过滤重复的学生。
496
+
470
497
``` mysql
471
498
# Write your MySQL query statement below
472
499
select class from courses group by class having count (distinct student) >= 5 ;
0 commit comments