美文网首页
SQL第11-14课:查询与联结

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

作者: 周运来就是我 | 来源:发表于2018-08-29 22:30 被阅读9次
  • 查询(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 返回包含重复的行。

相关文章

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

    查询(query): 子查询的select语句只能查询单个列。 关系数据库与非关系数据库 sql语句中出现笛卡尔乘...

  • 《mysql必知必会》读书实战笔记15-联结查询

    第15章 联结表 15.1联结 联结查询(join),是SQL最强大的功能之一。 15.1.1关系表 关系数据库,...

  • SQL必知必会读书笔记12

    基础知识 SQL可以在数据查询的执行中联结(join)表,联结是利用SQL的SELECT能执行的最重要的操作。 关...

  • need

    sql语句执行原理,顺序 存储过程使用场景 分组 联结查询 索引

  • 6-SQL联结查询(子查询、组合查询、联结查询)

    说明 ▍ 子查询:嵌套在其他查询中的查询; ▍ 组合查询(复合查询 / 并操作):执行多次查询,多条select语...

  • 【MySQL必知必会】第15章:

    15.1 联结 SQL最强大的功能之一就是能在数据检索查询的执行中联结(join)表,但在掌握联结之前需要了...

  • SQL入门笔记(中)

    主要内容:查询、联结、表与行列的增删 子查询 内联结 其他联结方式 复合查询 插入(行)数据 篡改和删除(行)数据...

  • SQL 学习笔记

    SQL基础 参考书籍: 《SQL必知必会》 检索数据 过滤数据 函数 分组数据 子查询 简单联结 法一: 法二: ...

  • MySQL白菜教程(Level 6)

    多表联结 1.联结SQL 最强大的功能之一就是能在数据检索查询的执行中联结表 什么叫联结例子如下: 在大学每一个新...

  • 联结与查询

    根据上图的关系创建4张表分别是订单表、用户表、订单详情表、商品表 订单表:订单id、订单时间、下单人 订单详情:订...

网友评论

      本文标题:SQL第11-14课:查询与联结

      本文链接:https://www.haomeiwen.com/subject/ofbvfftx.html