MariaDB JOIN

MariaDB JOIN 

JOIN两张表

(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   | prod_name      | prod_price |

+-------------+----------------+------------+

| ACME        | Bird seed          10.00 |

| ACME        | Carrots        |       2.50 |

| ACME        | Detonator          13.00 |

| ACME        | Safe               50.00 |

| ACME        | Sling          |       4.49 |

| ACME        | TNT (1 stick)  |       2.50 |

| ACME        | TNT (5 sticks) |      10.00 |

| Anvils R Us | .5 ton anvil   |       5.99 |

| Anvils R Us | 1 ton anvil    |       9.99 |

| Anvils R Us | 2 ton anvil        14.99 |

| Jet Set     | JetPack 1000       35.00 |

| Jet Set     | JetPack 2000       55.00 |

| LT Supplies | Fuses          |       3.42 |

| LT Supplies | Oil can        |       8.99 |

+-------------+----------------+------------+

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      | prod_name      | prod_price |

+----------------+----------------+------------+

| ACME           | .5 ton anvil   |       5.99 |

| ACME           | 1 ton anvil    |       9.99 |

| ACME           | 2 ton anvil        14.99 |

| ACME           | Bird seed          10.00 |

| ACME           | Carrots        |       2.50 |

| ACME           | Detonator          13.00 |

| ACME           | Fuses          |       3.42 |

| ACME           | JetPack 1000       35.00 |

| ACME           | JetPack 2000       55.00 |

| ACME           | Oil can        |       8.99 |

| ACME           | Safe               50.00 |

| ACME           | Sling          |       4.49 |

| ACME           | TNT (1 stick)  |       2.50 |

| ACME           | TNT (5 sticks) |      10.00 |

| Anvils R Us    | .5 ton anvil   |       5.99 |

| Anvils R Us    | 1 ton anvil    |       9.99 |

| Anvils R Us    | 2 ton anvil        14.99 |

| Anvils R Us    | Bird seed          10.00 |

| Anvils R Us    | Carrots        |       2.50 |

| Anvils R Us    | Detonator          13.00 |

| Anvils R Us    | Fuses          |       3.42 |

| Anvils R Us    | JetPack 1000       35.00 |

| Anvils R Us    | JetPack 2000       55.00 |

| Anvils R Us    | Oil can        |       8.99 |

| Anvils R Us    | Safe               50.00 |

| Anvils R Us    | Sling          |       4.49 |

| Anvils R Us    | TNT (1 stick)  |       2.50 |

| Anvils R Us    | TNT (5 sticks) |      10.00 |

| Furball Inc.   | .5 ton anvil   |       5.99 |

| Furball Inc.   | 1 ton anvil    |       9.99 |

| Furball Inc.   | 2 ton anvil        14.99 |

| Furball Inc.   | Bird seed          10.00 |

| Furball Inc.   | Carrots        |       2.50 |

| Furball Inc.   | Detonator          13.00 |

| Furball Inc.   | Fuses          |       3.42 |

| Furball Inc.   | JetPack 1000       35.00 |

| Furball Inc.   | JetPack 2000       55.00 |

| Furball Inc.   | Oil can        |       8.99 |

| Furball Inc.   | Safe               50.00 |

| Furball Inc.   | Sling          |       4.49 |

| Furball Inc.   | TNT (1 stick)  |       2.50 |

| Furball Inc.   | TNT (5 sticks) |      10.00 |

| Jet Set        | .5 ton anvil   |       5.99 |

| Jet Set        | 1 ton anvil    |       9.99 |

| Jet Set        | 2 ton anvil        14.99 |

| Jet Set        | Bird seed          10.00 |

| Jet Set        | Carrots        |       2.50 |

| Jet Set        | Detonator          13.00 |

| Jet Set        | Fuses          |       3.42 |

| Jet Set        | JetPack 1000       35.00 |

| Jet Set        | JetPack 2000       55.00 |

| Jet Set        | Oil can        |       8.99 |

| Jet Set        | Safe               50.00 |

| Jet Set        | Sling          |       4.49 |

| Jet Set        | TNT (1 stick)  |       2.50 |

| Jet Set        | TNT (5 sticks) |      10.00 |

| Jouets Et Ours | .5 ton anvil   |       5.99 |

| Jouets Et Ours | 1 ton anvil    |       9.99 |

| Jouets Et Ours | 2 ton anvil        14.99 |

| Jouets Et Ours | Bird seed          10.00 |

| Jouets Et Ours | Carrots        |       2.50 |

| Jouets Et Ours | Detonator          13.00 |

| Jouets Et Ours | Fuses          |       3.42 |

| Jouets Et Ours | JetPack 1000       35.00 |

| Jouets Et Ours | JetPack 2000       55.00 |

| Jouets Et Ours | Oil can        |       8.99 |

| Jouets Et Ours | Safe               50.00 |

| Jouets Et Ours | Sling          |       4.49 |

| Jouets Et Ours | TNT (1 stick)  |       2.50 |

| Jouets Et Ours | TNT (5 sticks) |      10.00 |

| LT Supplies    | .5 ton anvil   |       5.99 |

| LT Supplies    | 1 ton anvil    |       9.99 |

| LT Supplies    | 2 ton anvil        14.99 |

| LT Supplies    | Bird seed          10.00 |

| LT Supplies    | Carrots        |       2.50 |

| LT Supplies    | Detonator          13.00 |

| LT Supplies    | Fuses          |       3.42 |

| LT Supplies    | JetPack 1000       35.00 |

| LT Supplies    | JetPack 2000       55.00 |

| LT Supplies    | Oil can        |       8.99 |

| LT Supplies    | Safe               50.00 |

| LT Supplies    | Sling          |       4.49 |

| LT Supplies    | TNT (1 stick)  |       2.50 |

| LT Supplies    | TNT (5 sticks) |      10.00 |

+----------------+----------------+------------+

 

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   | prod_name      | prod_price |

+-------------+----------------+------------+

| Anvils R Us | .5 ton anvil   |       5.99 |

| Anvils R Us | 1 ton anvil    |       9.99 |

| Anvils R Us | 2 ton anvil        14.99 |

| LT Supplies | Oil can        |       8.99 |

| LT Supplies | Fuses          |       3.42 |

| ACME        | Sling          |       4.49 |

| ACME        | TNT (1 stick)  |       2.50 |

| ACME        | TNT (5 sticks) |      10.00 |

| ACME        | Bird seed          10.00 |

| ACME        | Carrots        |       2.50 |

| ACME        | Safe               50.00 |

| ACME        | Detonator          13.00 |

| Jet Set     | JetPack 1000       35.00 |

| Jet Set     | JetPack 2000       55.00 |

+-------------+----------------+------------+

 

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      | vend_name   | prod_price | quantity |

+----------------+-------------+------------+----------+

| .5 ton anvil   | Anvils R Us |       5.99 |       10 |

| 1 ton anvil    | Anvils R Us |       9.99 |        3 |

| TNT (5 sticks) | ACME            10.00 |        5 |

| Bird seed      | ACME            10.00 |        1 |

+----------------+-------------+------------+----------+

 

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   | Sling          |

| TNT1    | TNT (1 stick)  |

| TNT2    | TNT (5 sticks) |

| FB      | Bird seed      |

| FC      | Carrots        |

| SAFE    | Safe           |

| DTNTR   | Detonator      |

+---------+----------------+

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   | Sling          |

| TNT1    | TNT (1 stick)  |

| TNT2    | TNT (5 sticks) |

| FB      | Bird seed      |

| FC      | Carrots        |

| SAFE    | Safe           |

| DTNTR   | Detonator      |

+---------+----------------+

 

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   | cust_address   | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email      | order_num | order_date          | prod_id | quantity | item_price |

+---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+

|   10001 | Coyote Inc. | 200 Maple Lane | Detroit   | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com |     20005 | 2011-09-01 00:00:00 | FB            1 |      10.00 |

|   10001 | Coyote Inc. | 200 Maple Lane | Detroit   | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com |     20009 | 2011-10-08 00:00:00 | FB            1 |      10.00 |

+---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+

 

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 |

+---------+-----------+

|   10001 |     20005 |

|   10003 |     20006 |

|   10004 |     20007 |

|   10005 |     20008 |

|   10001 |     20009 |

+---------+-----------+

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 |

+---------+-----------+

|   10001 |     20005 |

|   10003 |     20006 |

|   10004 |     20007 |

|   10005 |     20008 |

|   10001 |     20009 |

+---------+-----------+

 

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 |

+---------+-----------+

|   10001 |     20005 |

|   10001 |     20009 |

|   10002 |      NULL |

|   10003 |     20006 |

|   10004 |     20007 |

|   10005 |     20008 |

+---------+-----------+

 

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      | cust_id | num_ord |

+----------------+---------+---------+

| Coyote Inc.    |   10001 |       2 |

| Wascals        |   10003 |       1 |

| Yosemite Place |   10004 |       1 |

| E Fudd         |   10005 |       1 |

+----------------+---------+---------+

 

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      | cust_id | num_ord |

+----------------+---------+---------+

| Coyote Inc.    |   10001 |       2 |

| Wascals        |   10003 |       1 |

| Yosemite Place |   10004 |       1 |

| E Fudd         |   10005 |       1 |

+----------------+---------+---------+

 

4 rows in set (0.00 sec)

posted @ 2016-03-19 15:46  李庆喜  阅读(232)  评论(0编辑  收藏  举报