SQL第11-14课:查询与联结

  • 查询(query):
mysql> select cust_name,cust_contact
    -> from Customers
    -> where cust_id in ( select cust_id
    -> from Orders
    -> where order_num in (select order_num
    -> from OrderItems
    -> where prod_id='RGAN01'));
+---------------+--------------------+
| cust_name     | cust_contact       |
+---------------+--------------------+
| Fun4All       | Denise L. Stephens |
| The Toy Store | Kim Howard         |
+---------------+--------------------+
2 rows in set (0.01 sec)

子查询的select语句只能查询单个列。

关系数据库与非关系数据库

mysql> select vend_name,prod_name,prod_price
    -> from Vendors,Products
    -> where Vendors.vend_id=Products.vend_id;
+-----------------+---------------------+------------+
| vend_name       | prod_name           | prod_price |
+-----------------+---------------------+------------+
| Bears R Us      | 8 inch teddy bear   |       5.99 |
| Bears R Us      | 12 inch teddy bear  |       8.99 |
| Bears R Us      | 18 inch teddy bear  |      11.99 |
| Doll House Inc. | Fish bean bag toy   |       3.49 |
| Doll House Inc. | Bird bean bag toy   |       3.49 |
| Doll House Inc. | Rabbit bean bag toy |       3.49 |
| Doll House Inc. | Raggedy Ann         |       4.99 |
| Fun and Games   | King doll           |       9.49 |
| Fun and Games   | Queen doll          |       9.49 |
+-----------------+---------------------+------------+

sql语句中出现笛卡尔乘积 SQL查询入门篇

SQL第11-14课:查询与联结_第1张图片
图片.png
使用别名联结
mysql> 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='RGAN01';
+---------------+--------------------+
| cust_name     | cust_contact       |
+---------------+--------------------+
| Fun4All       | Denise L. Stephens |
| The Toy Store | Kim Howard         |
+---------------+--------------------+
2 rows in set (0.00 sec)
自联结
mysql> select c1.cust_id,c1.cust_name,c1.cust_contact
    -> from Customers as c1, Customers as c2
    -> where c1.cust_name = c2.cust_name
    -> and c2.cust_contact = 'Jim Jones';
+------------+-----------+--------------------+
| cust_id    | cust_name | cust_contact       |
+------------+-----------+--------------------+
| 1000000003 | Fun4All   | Jim Jones          |
| 1000000004 | Fun4All   | Denise L. Stephens |
+------------+-----------+--------------------+
2 rows in set (0.00 sec)
自然联结
mysql> 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 = 'RGAN01';

+------------+---------------+---------------------+-----------+------------+----------+--------------+--------------------+-----------------------+-----------+---------------------+---------+----------+------------+
| 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 |
+------------+---------------+---------------------+-----------+------------+----------+--------------+--------------------+-----------------------+-----------+---------------------+---------+----------+------------+
| 1000000004 | Fun4All       | 829 Riverside Drive | Phoenix   | AZ         | 88888    | USA          | Denise L. Stephens | [email protected] |     20007 | 2012-01-30 00:00:00 | RGAN01  |       50 |       4.49 |
| 1000000005 | The Toy Store | 4545 53rd Street    | Chicago   | IL         | 54545    | USA          | Kim Howard         | NULL                  |     20008 | 2012-02-03 00:00:00 | RGAN01  |        5 |       4.99 |
+------------+---------------+---------------------+-----------+------------+----------+--------------+--------------------+-----------------------+-----------+---------------------+---------+----------+------------+
外联结
mysql> select  Customers.cust_id,Orders.order_num
    -> from Customers inner join Orders
    -> on Customers.cust_id = Orders.cust_id;
+------------+-----------+
| cust_id    | order_num |
+------------+-----------+
| 1000000001 |     20005 |
| 1000000003 |     20006 |
| 1000000004 |     20007 |
| 1000000005 |     20008 |
| 1000000001 |     20009 |
+------------+-----------+
5 rows in set (0.00 sec)

mysql> select  Customers.cust_id,Orders.order_num from Customers left outer  join Orders on Customers.cust_id = Orders.cust_id;
+------------+-----------+
| cust_id    | order_num |
+------------+-----------+
| 1000000001 |     20005 |
| 1000000001 |     20009 |
| 1000000002 |      NULL |
| 1000000003 |     20006 |
| 1000000004 |     20007 |
| 1000000005 |     20008 |
+------------+-----------+
6 rows in set (0.00 sec)
使用带集聚函数的联结
mysql> select 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_id    | num_ord |
+------------+---------+
| 1000000001 |       2 |
| 1000000003 |       1 |
| 1000000004 |       1 |
| 1000000005 |       1 |
+------------+---------+
4 rows in set (0.00 sec)
mysql> select cust_name,cust_contact,cust_email
  -> from Customers
  -> where cust_state in ('IL','IN','MI');
+---------------+--------------+-----------------------+
| cust_name     | cust_contact | cust_email            |
+---------------+--------------+-----------------------+
| Village Toys  | John Smith   | [email protected] |
| Fun4All       | Jim Jones    | [email protected]    |
| The Toy Store | Kim Howard   | NULL                  |
+---------------+--------------+-----------------------+
mysql> select cust_name,cust_contact,cust_email
    -> from Customers
    -> where cust_name='Fun4All';
+-----------+--------------------+-----------------------+
| cust_name | cust_contact       | cust_email            |
+-----------+--------------------+-----------------------+
| Fun4All   | Jim Jones          | [email protected]    |
| Fun4All   | Denise L. Stephens | [email protected] |
+-----------+--------------------+-----------------------+
mysql> select cust_name,cust_contact,cust_email
    ->  from Customers
    ->  where cust_state in ('IL','IN','MI')
    -> or cust_name='Fun4All';
+---------------+--------------------+-----------------------+
| cust_name     | cust_contact       | cust_email            |
+---------------+--------------------+-----------------------+
| Village Toys  | John Smith         | [email protected] |
| Fun4All       | Jim Jones          | [email protected]    |
| Fun4All       | Denise L. Stephens | [email protected] |
| The Toy Store | Kim Howard         | NULL                  |
+---------------+--------------------+-----------------------+
mysql> select cust_name,cust_contact,cust_email
    -> from Customers
    -> where cust_state in ('IL','IN','MI')
    -> union
    ->  select cust_name,cust_contact,cust_email
    -> from Customers
    -> where cust_name='Fun4All';
+---------------+--------------------+-----------------------+
| cust_name     | cust_contact       | cust_email            |
+---------------+--------------------+-----------------------+
| Village Toys  | John Smith         | [email protected] |
| Fun4All       | Jim Jones          | [email protected]    |
| The Toy Store | Kim Howard         | NULL                  |
| Fun4All       | Denise L. Stephens | [email protected] |
+---------------+--------------------+-----------------------+

  • UNION 两条或两条以上select语句组成
  • union 中的每一个列必须包含相同的列、表达式、聚集函数
  • 列数据类型必须兼容:类型不必完全相同,但必须是DBMA可以隐含转换的类型

union all 返回包含重复的行。

你可能感兴趣的:(SQL第11-14课:查询与联结)