</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">SQLServer<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%AD%90%E6%9F%A5%E8%AF%A2&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">子查询</a>可以分为 相关<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%AD%90%E6%9F%A5%E8%AF%A2&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">子查询</a> 和 嵌套<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%AD%90%E6%9F%A5%E8%AF%A2&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">子查询</a> 两类。前提,
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">假设Books表如下:
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">类编号 图书名 出版社 价格 -------------------------------------------------------- 2 c#高级应用 <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%9C%A3%E9%80%9A&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">圣通</a>出版 23.00 2 Jsp开发应用 机械出版社 45.00 3 <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E9%AB%98%E7%AD%89%E6%95%B0%E5%AD%A6&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">高等数学</a> <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%B5%8E%E5%8D%97%E5%87%BA%E7%89%88%E7%A4%BE&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">济南出版社</a> 25.00 3 <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%96%AF%E7%8B%82%E8%8B%B1%E8%AF%AD&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">疯狂英语</a> <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%B8%85%E5%8D%8E%E5%A4%A7%E5%AD%A6%E5%87%BA%E7%89%88%E7%A4%BE&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">清华大学出版社</a> 32.00
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">嵌套子查询的执行不依赖与外部的查询。
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">执行过程:
(1)执行子查询,其结果不被显示,而是传递给外部查询,作为外部查询的条件使用。
(2)执行外部查询,并显示整个结果。
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">嵌套子查询一般可以分为:返回单值的子查询 和 返回一个列表的子查询 ,
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">下面举例说明:1.返<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%9B%9E%E5%8D%95&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">回单</a>值: --查询所有价格高于平均价格的图书名,作者,出版社和价格。 USE tempdb GO SElECT 图书名,作者,出版社,价格 FROM Books WHERE 价格 > ( SELECT AVG(价格) FROM Books ) GO2.返回值列表--查询所有借阅图书的读者信息 SElECT * FROM Readers WHERE 读者编号 IN ( SELECT 读者编号 FROM [Borrow History] ) GO
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">相关子查询的执行依赖于外部查询。多数情况下是子查询的WHERE子句中引用了外部查询的表。
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">执行过程:
(1)从外层查询中取出一个<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%85%83%E7%BB%84&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">元组</a>,将<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%85%83%E7%BB%84&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">元组</a>相关列的值传给内层查询。
(2)执行内层查询,得到子查询操作的值。
(3)外查询根据子查询返回的结果或<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%BB%93%E6%9E%9C%E9%9B%86&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">结果集</a>得到满足条件的行。
(4)然后外层查询取出下一个<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%85%83%E7%BB%84&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">元组</a>重复做步骤1-3,直到外层的元组全部处理完毕。
</pre><pre class="best-text mb-10" id="best-content-1232448527" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px; background-color: rgb(255, 255, 255);">下面举例说明:
查询Booka表中大于该<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%B1%BB%E5%9B%BE%E4%B9%A6&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">类图书</a>价格平均值的图书信息SElECT 图书名,出版社,类编号,价格
SELECT FROM Books As a WHERE 价格 > ( SELECT AVG(价格) FROM Books AS b WHERE a.类编号=b.类编号 ) GO 与前面介绍过的子查询不同,相关子查询无法独立于外部查询而得到解决。该子查询需要一个“类编号”的值。而这个值是个变量,随SQLSever检索Books表中的不同行而改变。下面详细说明该查询执行过程: 先将Books表中的第一条记录的“类编号”的值“2”代入子查询中,子查询变为: SELECT AVG(价格) FROM Books AS b WHERE b.类编号=2 子查询的结果为该<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%B1%BB%E5%9B%BE%E4%B9%A6&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">类图书</a>的平均价格,所以外部查询变为: SElECT 图书名,出版社,类编号,价格 FROM Books As a WHERE 价格 > 34 如果WHERE条件为True,则第一条结果包括在<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%BB%93%E6%9E%9C%E9%9B%86&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">结果集</a>中,则否不包括。对Books表中的所有行运行相同的过程,最后形成的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E7%BB%93%E6%9E%9C%E9%9B%86&fr=qb_search_exp&ie=utf8&eid_gfrom=151" rel="nofollow" style="text-decoration: none; color: rgb(12, 137, 207);">结果集</a>及最后返回结果。