深入了解子查询

Posted liangzaian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深入了解子查询相关的知识,希望对你有一定的参考价值。

 1 SELECT DISTINCT soh.OrderDate,sod.ProductID
 2 FROM Sales.SalesOrderHeader soh
 3 LEFT JOIN sales.SalesOrderDetail sod 
 4     ON soh.SalesOrderID =sod.SalesOrderID
 5 WHERE soh.OrderDate =(
 6     SELECT MIN(OrderDate) FROM Sales.SalesOrderHeader)
 7 
 8 --子查询
 9 SELECT e.BusinessEntityID,FirstName,LastName
10 FROM HumanResources.Employee e
11 JOIN Person.Person pp
12     ON e.BusinessEntityID=pp.BusinessEntityID
13 WHERE e.BusinessEntityID IN 
14 (SELECT DISTINCT BusinessEntityID FROM HumanResources.JobCandidate)
15 
16 --用连接代替子查询 JOIN 默认为INNER JOIN
17 SELECT e.BusinessEntityID,FirstName,LastName
18 FROM HumanResources.Employee e
19 JOIN Person.Person pp
20     ON e.BusinessEntityID=pp.BusinessEntityID
21 JOIN HumanResources.JobCandidate jc
22     ON jc.BusinessEntityID=e.BusinessEntityID
23 
24 -- 与NULL值比较总会得到NULL
25 SELECT e.BusinessEntityID,FirstName,LastName
26 FROM HumanResources.Employee e
27 JOIN Person.Person pp
28     ON e.BusinessEntityID=pp.BusinessEntityID
29 WHERE e.BusinessEntityID NOT IN 
30 (SELECT DISTINCT BusinessEntityID FROM HumanResources.JobCandidate 
31     WHERE BusinessEntityID IS NOT NULL)
32 
33 -- WHERE 子句中的相关子查询
34 --查询每一位客户在系统中的第一份订单的OrderID, OrderDate
35 /* 使用外面查询中的CustomerID,进行内部查询,得出一个客户的最早订单日期,返回给外部查询*/
36 
37 SELECT o1.CustomerID,o1.SalesOrderID,o1.OrderDate
38 FROM Sales.SalesOrderHeader o1
39 WHERE o1.OrderDate=(SELECT MIN(o2.OrderDate) 
40                     FROM Sales.SalesOrderHeader o2
41                     WHERE o2.CustomerID=o1.CustomerID ) -- 内部查询对外部的引用
42 ORDER BY o1.CustomerID   
43 
44 -- SELECT 子句中的相关子查询
45 SELECT pp.FirstName,pp.LastName,
46     (SELECT MIN(OrderDate)
47         FROM Sales.SalesOrderHeader ord_h
48         WHERE ord_h.CustomerID=c.CustomerID        
49     ) AS OrderDate
50 FROM Person.Person pp
51 JOIN Sales.Customer c
52     ON pp.BusinessEntityID=c.PersonID
53     
54 -- 派生表 派生表并不能解决所有问题, 结果相当庞大而且有大量记录要联结时,可能要考虑使用临时表并在其上构建索引(派生表没有索引)
55 -- 查询要求, 查询订过 迷你水泵 又订过 AWC Logo Cap 帽的所有客户
56 
57 SELECT DISTINCT    -- DISTINCT 去除重复订购的人
58         PP.FirstName ,
59         PP.LastName
60 FROM    Person.Person AS pp
61         JOIN ( SELECT   sc.PersonID
62                FROM     Sales.Customer sc
63                         JOIN Sales.SalesOrderHeader AS ord_h ON sc.CustomerID = ord_h.CustomerID
64                         JOIN Sales.SalesOrderDetail AS ord_d ON ord_d.SalesOrderID = ord_h.SalesOrderID
65                         JOIN Production.Product AS prd ON prd.ProductID = ord_d.ProductID
66                WHERE    prd.Name = Minipump
67              ) pumps ON pp.BusinessEntityID = pumps.PersonID
68         JOIN ( SELECT   sc.PersonID
69                FROM     Sales.Customer sc
70                         JOIN Sales.SalesOrderHeader AS ord_h ON sc.CustomerID = ord_h.CustomerID
71                         JOIN Sales.SalesOrderDetail AS ord_d ON ord_d.SalesOrderID = ord_h.SalesOrderID
72                         JOIN Production.Product AS prd ON prd.ProductID = ord_d.ProductID
73                WHERE    prd.Name = AWC Logo Cap
74              ) caps ON pp.BusinessEntityID = caps.PersonID
75              
76 -- Exists 运算符             
77 SELECT  e.BusinessEntityID ,
78         FirstName ,
79         LastName
80 FROM    HumanResources.Employee e
81         JOIN Person.Person pp ON e.BusinessEntityID = pp.BusinessEntityID
82 WHERE   EXISTS ( SELECT BusinessEntityID
83                  FROM   HumanResources.JobCandidate jc
84                  WHERE  e.BusinessEntityID = jc.BusinessEntityID )
85 
86  
87 
88 -- 以上摘自SQL Server 2008 高级程序设计

 

以上是关于深入了解子查询的主要内容,如果未能解决你的问题,请参考以下文章

如何破解子视图的清除?

你如何在 python 中处理 graphql 查询和片段?

片段视图返回后执行的 Firebase 查询

华为OD机试题,用 Java 解子序列长度问题

深入理解DOM节点类型第四篇——文档片段节点DocumentFragment

Microsoft SQL Server 代码片段收集