(jlive)[crashcourse]>SELECT order_num FROM orderitems WHERE prod_id = 'TNT2';
+-----------+
| order_num |
+-----------+
|
|
+-----------+
2 rows in set (0.00 sec)
order_num ==> cust_id
(jlive)[crashcourse]>SELECT cust_id FROM orders WHERE order_num IN (20005,20007);
+---------+
| cust_id |
+---------+
|
|
+---------+
2 rows in set (0.00 sec)
cust_id ==> cust_name,cust_contact
(jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers WHERE cust_id IN (10001,10004);
+----------------+--------------+
| cust_name
+----------------+--------------+
| Coyote Inc.
| Yosemite Place | Y Sam
+----------------+--------------+
2 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT
cust_name, cust_contact FROM customers WHERE cust_id IN (SELECT cust_id FROM orders WHERE
order_num
+----------------+--------------+
| cust_name
+----------------+--------------+
| Coyote Inc.
| Yosemite Place | Y Sam
+----------------+--------------+
2 rows in set (0.01 sec)
或
(jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num AND prod_id = 'TNT2';
+----------------+--------------+
| cust_name
+----------------+--------------+
| Coyote Inc.
| Yosemite Place | Y Sam
+----------------+--------------+
2 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers AS c, orders AS o, orderitems AS oi WHERE c.cust_id = o.cust_id AND oi.order_num = o.order_num AND prod_id = 'TNT2';
+----------------+--------------+
| cust_name
+----------------+--------------+
| Coyote Inc.
| Yosemite Place | Y Sam
+----------------+--------------+
2 rows in set (0.00 sec)
子查询混合计算
(jlive)[crashcourse]>SELECT COUNT(*) AS orders FROM orders WHERE cust_id = 10001;
+--------+
| orders |
+--------+
|
+--------+
1 row in set (0.00 sec)
(jlive)[crashcourse]>SELECT
cust_id,
+---------+----------------+------------+--------+
| cust_id | cust_name
+---------+----------------+------------+--------+
|
|
|
|
|
+---------+----------------+------------+--------+
5 rows in set (0.00 sec)