-
Notifications
You must be signed in to change notification settings - Fork 35
/
14-working-with-subqueries.sql
73 lines (68 loc) · 2.74 KB
/
14-working-with-subqueries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- 第14章 使用子查询
SELECT * FROM orderitems;
-- +-----------+------------+---------+----------+------------+
-- | order_num | order_item | prod_id | quantity | item_price |
-- +-----------+------------+---------+----------+------------+
-- | 20005 | 1 | ANV01 | 10 | 5.99 |
-- | 20005 | 2 | ANV02 | 3 | 9.99 |
-- | 20005 | 3 | TNT2 | 5 | 10.00 |
-- | 20005 | 4 | FB | 1 | 10.00 |
-- | 20006 | 1 | JP2000 | 1 | 55.00 |
-- | 20007 | 1 | TNT2 | 100 | 10.00 |
-- | 20008 | 1 | FC | 50 | 2.50 |
-- | 20009 | 1 | FB | 1 | 10.00 |
-- | 20009 | 2 | OL1 | 1 | 8.99 |
-- | 20009 | 3 | SLING | 1 | 4.49 |
-- | 20009 | 4 | ANV03 | 1 | 14.99 |
-- +-----------+------------+---------+----------+------------+
SELECT * FROM orders;
-- +-----------+---------------------+---------+
-- | order_num | order_date | cust_id |
-- +-----------+---------------------+---------+
-- | 20005 | 2005-09-01 00:00:00 | 10001 |
-- | 20006 | 2005-09-12 00:00:00 | 10003 |
-- | 20007 | 2005-09-30 00:00:00 | 10004 |
-- | 20008 | 2005-10-03 00:00:00 | 10005 |
-- | 20009 | 2005-10-08 00:00:00 | 10001 |
-- +-----------+---------------------+---------+
-- sub query
SELECT cust_id
FROM orders
WHERE order_num IN (
SELECT order_num
FROM orderitems
WHERE prod_id = 'TNT2'
);
-- +---------+
-- | cust_id |
-- +---------+
-- | 10001 |
-- | 10004 |
-- +---------+
SELECT cust_id, cust_name, cust_state FROM customers;
-- +---------+----------------+------------+
-- | cust_id | cust_name | cust_state |
-- +---------+----------------+------------+
-- | 10001 | Coyote Inc. | MI |
-- | 10002 | Mouse House | OH |
-- | 10003 | Wascals | IN |
-- | 10004 | Yosemite Place | AZ |
-- | 10005 | E Fudd | IL |
-- +---------+----------------+------------+
SELECT cust_name,
cust_state,
( SELECT Count(*)
FROM orders
WHERE orders.cust_id = customers.cust_id
) AS num_orders
FROM customers
ORDER BY cust_name;
-- +----------------+------------+------------+
-- | cust_name | cust_state | num_orders |
-- +----------------+------------+------------+
-- | Coyote Inc. | MI | 2 |
-- | E Fudd | IL | 1 |
-- | Mouse House | OH | 0 |
-- | Wascals | IN | 1 |
-- | Yosemite Place | AZ | 1 |
-- +----------------+------------+------------+