码迷,mamicode.com
首页 > 数据库 > 详细

SQL 查询基础(2)-实例

时间:2014-07-22 23:17:15      阅读:475      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   数据   

上次说到SQL查询语句的逻辑执行过程,现在来用一个实例说明一下逻辑执行的过程。

前提:我们有三个表,分别记住客户信息、订单信息和产品信息

客户信息表:Customer

ID,Name,Adress,PhoneNumber

ID Name Adress PhoneNumber
1 CompanyA No.1 Street 123456
2 CompanyB No.2 Street 23453
3 CompanyC No.3 Street 45321
4 CompanyD No.4 Street 4567890
5 CompanyE No.5 Street 123890

订单信息表:OrderInfo 

ID,Cus_ID,Product_ID,OrderDate,Qua

ID Cus_ID Product_ID OrderDate Qua
1 2 2 1/1/2012 1000
2 4 1 5/2/2012 500
3 1 5 9/10/2013 3069

产品信息表:Product

ID,Name,Price

ID Name Price
1 ABC 10
2 DEF 16
3 GHI 18
4 JKL 9
5 MNO 100
mamicode.com,码迷
 1 SELECT 
 2        C.Name AS CustomerName, 
 3        O.OrderDate AS OrderDate, 
 4        O.Qua,
 5        P.Name AS ProductName,
 6        P.Price AS Price
 7   FROM Customer C
 8        INNER JOIN OrderInfo O
 9             ON C.ID = O.Cus_ID
10       INNER JOIN Product P
11           ON O.Product_ID = P.ID
mamicode.com,码迷

现在我们根据上一篇文章中的逻辑执行顺序分析一下查询的结果。

1) FROM Customer 表作为主表 T1

2) JOIN OrderInfo 表作为表 T2,这时将 T1和T2做笛卡尔乘积。得到的虚拟表如下:

ID Name Adress PhoneNumber ID Cus_ID Product_ID OrderDate Qua
1 CompanyA No.1 Street 123456 1 2 2 1/1/2012 1000
2 CompanyB No.2 Street 23453 1 2 2 1/1/2012 1000
3 CompanyC No.3 Street 45321 1 2 2 1/1/2012 1000
4 CompanyD No.4 Street 4567890 1 2 2 1/1/2012 1000
5 CompanyE No.5 Street 123890 1 2 2 1/1/2012 1000
1 CompanyA No.1 Street 123456 2 4 1 5/2/2012 500
2 CompanyB No.2 Street 23453 2 4 1 5/2/2012 500
3 CompanyC No.3 Street 45321 2 4 1 5/2/2012 500
4 CompanyD No.4 Street 4567890 2 4 1 5/2/2012 500
5 CompanyE No.5 Street 123890 2 4 1 5/2/2012 500
1 CompanyA No.1 Street 123456 3 1 5 9/10/2013 3069
2 CompanyB No.2 Street 23453 3 1 5 9/10/2013 3069
3 CompanyC No.3 Street 45321 3 1 5 9/10/2013 3069
4 CompanyD No.4 Street 4567890 3 1 5 9/10/2013 3069
5 CompanyE No.5 Street 123890 3 1 5 9/10/2013 3069

 执行 ON 后边的条件:C.ID = O.Cus_ID,保留结果为 TRUE 的记录,结果如下:

ID Name Adress PhoneNumber ID Cus_ID Product_ID OrderDate Qua Result
1 CompanyA No.1 Street 123456 1 2 2 1/1/2012 1000 FALSE
2 CompanyB No.2 Street 23453 1 2 2 1/1/2012 1000 TRUE
3 CompanyC No.3 Street 45321 1 2 2 1/1/2012 1000 FALSE
4 CompanyD No.4 Street 4567890 1 2 2 1/1/2012 1000 FALSE
5 CompanyE No.5 Street 123890 1 2 2 1/1/2012 1000 FALSE
1 CompanyA No.1 Street 123456 2 4 1 5/2/2012 500 FALSE
2 CompanyB No.2 Street 23453 2 4 1 5/2/2012 500 FALSE
3 CompanyC No.3 Street 45321 2 4 1 5/2/2012 500 FALSE
4 CompanyD No.4 Street 4567890 2 4 1 5/2/2012 500 TRUE
5 CompanyE No.5 Street 123890 2 4 1 5/2/2012 500 FALSE
1 CompanyA No.1 Street 123456 3 1 5 9/10/2013 3069 TRUE
2 CompanyB No.2 Street 23453 3 1 5 9/10/2013 3069 FALSE
3 CompanyC No.3 Street 45321 3 1 5 9/10/2013 3069 FALSE
4 CompanyD No.4 Street 4567890 3 1 5 9/10/2013 3069 FALSE
5 CompanyE No.5 Street 123890 3 1 5 9/10/2013 3069 FALSE

由于用的是 INNER JOIN,所以不考虑 T1(T2)中存在而T2(T1)中不存在的情况,继续执行笛卡尔乘积和 ON 条件筛选,得到结果如下表:

ID Name Adress PhoneNumber ID Cus_ID Product_ID OrderDate Qua ID Name Price
4 CompanyD No.4 Street 4567890 2 4 1 5/2/2012 500 1 ABC 10
2 CompanyB No.2 Street 23453 1 2 2 1/1/2012 1000 2 DEF 16
1 CompanyA No.1 Street 123456 3 1 5 9/10/2013 3069 5 MNO 100

由于没有用到 WHERE 条件,没有 GROUP BY,没有 HAVING 之类的操作。直接执行 SELECT 中需要选中的列,返回数据查询结果。

CustomerName OrderDate Qua ProductName Price
CompanyD 5/2/2012 500 ABC 10
CompanyB 1/1/2012 1000 DEF 16
CompanyA 9/10/2013 3069 MNO 100

至此,SQL 的逻辑执行过程处理完毕。

SQL 查询基础(2)-实例,码迷,mamicode.com

SQL 查询基础(2)-实例

标签:style   blog   http   java   color   数据   

原文地址:http://www.cnblogs.com/Danielhu1983/p/3698690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!