1 子查询
1 | -- Find products that are more |
2 IN 运算符
1 | -- Find the products that have never been ordered |
3 子查询 VS 连接
1 | -- Find clients without invoices |
用连接的方法:
1 | SELECT * |
在时间一样的情况下, 选择更易读的那种方法
4 ALL 关键字
1 | -- Select invoices larger than all invoices of client 3 |
改用all 关键字:
1 | SELECT * |
5 ANY 关键字
1 | -- Select clients with at least two invoices |
改用any 关键字
1 | -- Select clients with at least two invoices |
即 “IN” 相当于 “=ANY”
6 相关子查询
1 | -- Select employees whose salary is |
注意到子查询用到了外查询的 e.office_id, 也就是说子查询和外查询是相关子查询.
对于非相关子查询, 执行一次就可以了.
但是对于相关子查询, 外查询每一条record都要执行一次子查询,所以执行时间相对较长.
7 EXISTS 运算符
1 | -- Select clients that have an invoice |
子查询返回一个列表, 然后外查询看在不在此列表中.如果子查询返回的列表非常大,就会很影响性能.
使用EXISTS运算符
1 | -- Select clients that have an invoice |
对于外查询的每一条record, EXISTS 子查询返回TRUE / FALSE, 若返回TRUE,就添加到结果集中.
Exercise
1 | -- Find the products that have never been ordered |
8 SELECT 子句中的子查询
1 | SELECT |
得到这样一张表:
invoice_id | invoice_total | invoice_average | difference |
---|---|---|---|
1 | 101.79 | 152.388235 | -50.598235 |
2 | 175.32 | 152.388235 | 22.931765 |
3 | 147.99 | 152.388235 | -4.398235 |
4 | 152.21 | 152.388235 | -0.178235 |
5 | 169.36 | 152.388235 | 16.971765 |
6 | 157.78 | 152.388235 | 5.391765 |
7 | 133.87 | 152.388235 | -18.518235 |
Exercise
得到这样一张表:
client_id | name | total_sales | average | difference |
---|---|---|---|---|
1 | Vinte | 802.89 | 152.388235 | 650.501765 |
2 | Myworks | 101.79 | 152.388235 | -50.598235 |
3 | Yadel | 705.90 | 152.388235 | 553.511765 |
4 | Kwideo | 152.388235 | ||
5 | Topiclounge | 980.02 | 152.388235 | 827.631765 |
Solution:
1 | SELECT |
9 FROM 子句中的子查询
将上面的查询结果当成表来用,放在FROM 子句中:
1 | SELECT * |
得到:
client_id | name | total_sales | average | difference |
---|---|---|---|---|
1 | Vinte | 802.89 | 152.388235 | 650.501765 |
2 | Myworks | 101.79 | 152.388235 | -50.598235 |
3 | Yadel | 705.90 | 152.388235 | 553.511765 |
5 | Topiclounge | 980.02 | 152.388235 | 827.631765 |