(jlive)[crashcourse]>SELECT vend_name, prod_name, prod_price FROM vendors, products WHERE vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;
+-------------+----------------+------------+
| vend_name
+-------------+----------------+------------+
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| Anvils R Us | .5 ton anvil
| Anvils R Us | 1 ton anvil
| Anvils R Us | 2 ton anvil
| Jet Set
| Jet Set
| LT Supplies | Fuses
| LT Supplies | Oil can
+-------------+----------------+------------+
14 rows in set (0.01 sec)
如果不加两张表的关联条件,则查询结果有误
(jlive)[crashcourse]>SELECT vend_name, prod_name, prod_price FROM vendors, products ORDER BY vend_name, prod_name;
+----------------+----------------+------------+
| vend_name
+----------------+----------------+------------+
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Anvils R Us
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Furball Inc.
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jet Set
| Jouets Et Ours | .5 ton anvil
| Jouets Et Ours | 1 ton anvil
| Jouets Et Ours | 2 ton anvil
| Jouets Et Ours | Bird seed
| Jouets Et Ours | Carrots
| Jouets Et Ours | Detonator
| Jouets Et Ours | Fuses
| Jouets Et Ours | JetPack 1000
| Jouets Et Ours | JetPack 2000
| Jouets Et Ours | Oil can
| Jouets Et Ours | Safe
| Jouets Et Ours | Sling
| Jouets Et Ours | TNT (1 stick)
| Jouets Et Ours | TNT (5 sticks) |
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
| LT Supplies
+----------------+----------------+------------+
84 rows in set (0.00 sec)
INNER JOIN两张表
(jlive)[crashcourse]>SELECT vend_name, prod_name, prod_price FROM vendors INNER JOIN products ON vendors.vend_id = products.vend_id;
+-------------+----------------+------------+
| vend_name
+-------------+----------------+------------+
| Anvils R Us | .5 ton anvil
| Anvils R Us | 1 ton anvil
| Anvils R Us | 2 ton anvil
| LT Supplies | Oil can
| LT Supplies | Fuses
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| ACME
| Jet Set
| Jet Set
+-------------+----------------+------------+
14 rows in set (0.00 sec)
比WHERE效率更高,性能更好
JOIN多张表
(jlive)[crashcourse]>SELECT prod_name, vend_name, prod_price, quantity FROM orderitems, products, vendors WHERE products.vend_id = vendors.vend_id AND orderitems.prod_id = products.prod_id AND order_num = 20005;
+----------------+-------------+------------+----------+
| prod_name
+----------------+-------------+------------+----------+
| .5 ton anvil
| 1 ton anvil
| TNT (5 sticks) | ACME
| Bird seed
+----------------+-------------+------------+----------+
4 rows in set (0.00 sec)
Self JOIN
(jlive)[crashcourse]>SELECT prod_id, prod_name FROM products WHERE vend_id = (SELECT vend_id FROM products WHERE prod_id = 'DTNTR');
+---------+----------------+
| prod_id | prod_name
+---------+----------------+
| SLING
| TNT1
| TNT2
| FB
| FC
| SAFE
| DTNTR
+---------+----------------+
7 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT p1.prod_id, p1.prod_name FROM products AS p1, products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id = 'DTNTR';
+---------+----------------+
| prod_id | prod_name
+---------+----------------+
| SLING
| TNT1
| TNT2
| FB
| FC
| SAFE
| DTNTR
+---------+----------------+
7 rows in set (0.00 sec)
同一张表做查询,妙用别名
Natural JOIN
(jlive)[crashcourse]>SELECT c.*, o.order_num, o.order_date, oi.prod_id, oi.quantity, oi.item_price 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 = 'FB';
+---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+
| cust_id | cust_name
+---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+
|
|
+---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+
2 rows in set (0.00 sec)
OUTER JOIN
(jlive)[crashcourse]>SELECT customers.cust_id, orders.order_num FROM customers INNER JOIN orders ON customers.cust_id = orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|
|
|
|
|
+---------+-----------+
5 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT customers.cust_id, orders.order_num FROM customers RIGHT OUTER JOIN orders ON customers.cust_id = orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|
|
|
|
|
+---------+-----------+
5 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT customers.cust_id, orders.order_num FROM customers LEFT OUTER JOIN orders ON customers.cust_id = orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|
|
|
|
|
|
+---------+-----------+
6 rows in set (0.00 sec)
Inner Join的两张表都有相关的行,Outer Join既包含两张表的相关行也包含不相关的行,故有左右之分,必须使用LEFT(或RIGHT),LEFT是以JOIN左边的表(customers)为基准,RIGHT是以JOIN右边的表(orders)为基准,始终查询显示完LEFT(或RIGHT)为止
JOIN with Aggregate
(jlive)[crashcourse]>SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_num) AS num_ord FROM customers INNER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;
+----------------+---------+---------+
| cust_name
+----------------+---------+---------+
| Coyote Inc.
| Wascals
| Yosemite Place |
| E Fudd
+----------------+---------+---------+
4 rows in set (0.00 sec)
(jlive)[crashcourse]>SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_num) AS num_ord FROM customers RIGHT OUTER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;
+----------------+---------+---------+
| cust_name
+----------------+---------+---------+
| Coyote Inc.
| Wascals
| Yosemite Place |
| E Fudd
+----------------+---------+---------+
4 rows in set (0.00 sec)