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查询入门篇

图片.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 | dstephens@fun4all.com |     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   | sales@villagetoys.com |
| Fun4All       | Jim Jones    | jjones@fun4all.com    |
| 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          | jjones@fun4all.com    |
| Fun4All   | Denise L. Stephens | dstephens@fun4all.com |
+-----------+--------------------+-----------------------+
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         | sales@villagetoys.com |
| Fun4All       | Jim Jones          | jjones@fun4all.com    |
| Fun4All       | Denise L. Stephens | dstephens@fun4all.com |
| 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         | sales@villagetoys.com |
| Fun4All       | Jim Jones          | jjones@fun4all.com    |
| The Toy Store | Kim Howard         | NULL                  |
| Fun4All       | Denise L. Stephens | dstephens@fun4all.com |
+---------------+--------------------+-----------------------+

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

union all 返回包含重复的行。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 表 存储在表中的数据是同一种类型的数据或清单。 数据库中的表有为一个名字来标识自己。 表具有一些特性,这些特性定义...
    蛐蛐囍阅读 1,350评论 0 7
  • 第三课: 排序检索数据 distinct关键字:distinct 列名1,列名2,列名3DISTINCT 关键字会...
    VictorBXv阅读 1,525评论 0 8
  • 前言 读《sql必知必会 第四版》随手做的笔记,写的比较乱,可读性并不好,读的是中文版,翻译过来的感觉有点怪怪的。...
    _老徐_阅读 662评论 0 0
  • 一、提高架构的性能 1.缓存系统: 前端:浏览器、网络,后端:服务、数据库、文件系统、硬盘和 CPU,全都有缓存,...
    hedgehog1112阅读 1,816评论 0 9
  • 看到朋友传来她的朋友圈传来的图片,无比揪心,两处受伤,伤口还不小。我的第一反应是大哥哥或小宝跟其他的小孩子打架或者...
    李朋希阅读 198评论 1 0