MySQL常用命令(九)-表的联结

外键

作用:作为某个表的一列,它包含另外一张表的主键值,定义了两个表之间的关系;

联结

作用:在一条SELECT中检索出多个关联表的数据。

mysql> SELECT * FROM vendors;
+----+-----------------+
| id | name            |
+----+-----------------+
|  1 | 平价蔬菜棚      |
|  2 | 天天好心情      |
+----+-----------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM product;
+----+-------------+-------+-----------+
| id | productName | price | vendorsId |
+----+-------------+-------+-----------+
|  3 | 豆角        |    10 |         1 |
+----+-------------+-------+-----------+
1 row in set (0.00 sec)

mysql> SELECT vendors.id ,vendors.`name` ,product.id, product.productName FROM vendors , product;#笛卡尔积;
+----+-----------------+----+-------------+
| id | name            | id | productName |
+----+-----------------+----+-------------+
|  1 | 平价蔬菜棚      |  3 | 豆角        |
|  2 | 天天好心情      |  3 | 豆角        |
+----+-----------------+----+-------------+
2 rows in set (0.00 sec)

mysql> SELECT vendors.id ,vendors.`name`, product.id ,product.productName FROM vendors , product WHERE vendors.id = product.vendorsId
    -> ;
+----+-----------------+----+-------------+
| id | name            | id | productName |
+----+-----------------+----+-------------+
|  1 | 平价蔬菜棚      |  3 | 豆角        |
+----+-----------------+----+-------------+
1 row in set (0.00 sec)

内联结的两种形式

mysql> SELECT product.productName,product.price,vendors.name FROM product,vendors WHERE product.vendorsId = vendors.id
    -> ;
+-------------+-------+-----------------+
| productName | price | name            |
+-------------+-------+-----------------+
| 豆角        |    10 | 平价蔬菜棚      |
+-------------+-------+-----------------+
1 row in set (0.00 sec)

mysql> SELECT product.productName,product.price,vendors.name FROM product INNER JOIN vendors ON product.vendorsId = vendors.id;
+-------------+-------+-----------------+
| productName | price | name            |
+-------------+-------+-----------------+
| 豆角        |    10 | 平价蔬菜棚      |
+-------------+-------+-----------------+
1 row in set (0.00 sec)

多表联结

多表联结会降低性能,所以尽量减少表的联结。

mysql> SELECT * FROM test.order;
+----+-----------+------------+---------+
| id | productId | customerId | quality |
+----+-----------+------------+---------+
|  1 |         3 |          1 |       2 |
+----+-----------+------------+---------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test.product;
+----+-------------+-------+-----------+
| id | productName | price | vendorsId |
+----+-------------+-------+-----------+
|  3 | 豆角        |    10 |         1 |
+----+-------------+-------+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test.customer;
+----+--------+-------------+
| id | name   | tel         |
+----+--------+-------------+
|  1 | 张三   | 15377294698 |
|  2 | 李四   | 15927955409 |
|  3 | 王五   | 15927955403 |
+----+--------+-------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM test.vendors;
+----+-----------------+
| id | name            |
+----+-----------------+
|  1 | 平价蔬菜棚      |
|  2 | 天天好心情      |
+----+-----------------+
2 rows in set (0.00 sec)

mysql> SELECT product.productName,product.price,order.quality,customer.name,vendors.name 
    -> FROM test.order,test.product,test.customer,test.vendors WHERE order.productId = product.id AND order.customerId = customer.id AND vendors.id = product.vendorsId;
+-------------+-------+---------+--------+-----------------+
| productName | price | quality | name   | name            |
+-------------+-------+---------+--------+-----------------+
| 豆角        |    10 |       2 | 张三   | 平价蔬菜棚      |
+-------------+-------+---------+--------+-----------------+
1 row in set (0.00 sec)

你可能感兴趣的:(MySQL常用命令(九)-表的联结)