SQL学习之学会使用子查询
1、SELECT语句是SQL的查询。我之前的随笔中所用的SELECT语句都是简单的查询,即从单个数据库表中检索数据的单条SELECT语句。
查询:任何SQL语句都是查询,但此术语一般指SELECT语句。
2、SQL不仅允许简单的SELECT查询,还允许创建子查询,即嵌套在其他查询中的查询。
下面通过实例来了解子查询在实际项目中的应用:
create database Study
go
use Study
go
create table Customers(
Id int identity(1,1),
Name varchar(10) null
)
insert into Customers values('张三')
insert into Customers values('李四')
insert into Customers values('王五')
insert into Customers values('赵六')
insert into Customers values('冯七')
select * from Customers
create table Products(
Id int identity(1,1),
Name varchar(255) null
)
insert into Products values('产品一')
insert into Products values('产品二')
insert into Products values('产品三')
insert into Products values('产品四')
insert into Products values('产品五')
select * from Products
create table Orders(
Id int identity(1,1),
CustomerId int null,
AddDate datetime null
)
insert into Orders values(1,GETDATE())
insert into Orders values(2,GETDATE())
insert into Orders values(3,GETDATE())
insert into Orders values(4,GETDATE())
insert into Orders values(5,GETDATE())
select * from Orders
create table OrderItems
(
Id int identity(1,1),
OrderId int null,
ProductId int null
)
insert into OrderItems values(1,1)
insert into OrderItems values(1,2)
insert into OrderItems values(1,3)
insert into OrderItems values(2,3)
insert into OrderItems values(3,2)
insert into OrderItems values(4,1)
select * from OrderItems
这是分析SQL子查询所需要用到的sql文件。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUUAAAH2CAIAAABoSAnRAAAgAElEQVR4nO2dzU7jyP7+61LOFWSFWtXcwdnC9rBxUG4AcfaH3SRpCST+t8AG0TggcQ0sIo0UXjZIZ6Znenr6rHj5DQnQQfNf2Ekcu97scrle8pQ+QsE43/qWqx7KcVyPyd/+lJ/6/w8AIIDYFikKCkpthRyfnAMAwoAcn5wb/Yfx9PTkSBDNBIbffwbAcaBn1QSsdxUAUozr+fHx0ZEgmglY7yoApEDPqglY7yoApNjQ893++vr+nXiLShCSeY8sgmaBnoEXGNfzw8NDflN5PbODELJQtGE9Pzw8WO8qAKQY1/P9/X1+U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y/oXib0P7V5HU8ue1SEsUG/IYWp8ryQS8ehbacfaTasOi1UDln4DU8/zCG91Dd/mELpCelbs6E8v8yjolZ7UgCj5H4k+hblDSlZ7f8et0Us+KRBP7C1vOoS0k0eB6/PI8HEaHdq/r9hjIXgQ83qn5f5a6fvmM2vfKcQRBw/YY+R+nFKnN+Q+qmPLUEqR1eVnZdkCofSRAA8BsKLavwcgbqwG8otKzCyxmoA7+h0LIKL2egDvyGQssqvJyBOvAbCi2r8HIG6sBvaOWK9TEHzIH1z6FlFV7OQB3oObSswssZqAM9h5ZVeDkDdaDn0LIKL2egDl/Pc8chPZ8S0QA62lQ05ZGOwoMth+73nGHBMkU7Z+A3Iv+wRMgXHS0DMf4AOtyYLVqWqlEyCi93Wy7dv515Wj30DBqFo2dNEavoeeFnIEc4Cs921lobWxZsfTA/A9dg+w1d9z+S9pmOzZDUbyiz1FGO5KR969CKTRf0DFyD7U9y1aO0vZ1aiLUHlc1JBP4kp3ut1tZmS/fzc6oZ6LmmnIHfcPVs2j8s4+BR3T9sLmPouaacgd9w/Ibi9txjyJTf0NHmXIFV/Ybml53sOAdAz8A1eP5hg4i0T8cvz+ObLiVRXL/fUGa46/p7DjE/15Yz8Buu39Bfp+101otiQ35DNX7/DD3XlDPwG/gNhZZVeDkDdeA3FFpW4eUM1IHfUGhZhZczUAd+Q6FlFV7OQB34DYWWVXg5A3XgN7RaxfqAA0Yhxyfn1pMAANQC9AxAOEDPAITD/wdNGkpOfkWrPAAAAABJRU5ErkJggg==" alt="" />
这是4个表的基础数据
需求:现在我们需要列出订购产品Id为1的所有顾客,下面是基本的思路:
(1)先去OrderItems(中间表)检索产品Id为'1'的所有的订单编号,代码如下:
select Id,orderId from OrderItems where ProductId=1
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHMAAAA9CAIAAAARc8GtAAAECUlEQVR4nO2by27aQBSGz6P0CVihaMobdAvbsjEoLxCl+2YXIBKR6CuwqRoMlfIMXVSqxCUbpOaetCuME25m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Bio3UkKtmgh0B6VcJ80ITpKQSRzRQLGd591rx/uGNk+bGuJ2vtBp2DrLnhRl2JDVRgsh5PIcyDECE9Bf8KmgWbBZNLgIj9YwMzXg0uB1m5BNw+DYPsygjYvGftFLdnXW7NmfSiF/YqfR63JrCI+P6fFZcsqluDvZLXzyhkMd2aRJNFdWsSTRbVrUk0WVS3JtFk04gU67mvyG69Bd5Y/geLxvACE8NuqQAAAABJRU5ErkJggg==" alt="" />
(2)拿到(1)步骤所有的订单Id编号之后,再根据检索出来的订单Id,去Orders(订单表)查找对应的顾客Id(是那些顾客下了这些订单),代码如下:
select CustomerId from Orders where Id in (select orderId from OrderItems where ProductId=1)
注意下面这种写法是错的:
select CustomerId from Orders where Id in (select Id,orderId from OrderItems where ProductId=1)
这就相当于判断Id==Id,orderId这种写法是错的!
DMBS控制台报的错误信息:当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。告诉你子查询中的选择列表只能指定一个表达式!
(3)拿到所有顾客Id之后,再根据这些顾客Id,去Customers(顾客表)中检索顾客的详细信息,代码如下:
select * from Customers Where Id in (select CustomerId from Orders where Id in (select orderId from OrderItems where ProductId=1))
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAA9CAIAAADUAVs8AAAETklEQVR4nO2azW6bQBDH51H6BD5F0TZv0GtybS5g5QWi9t7cYhPJkfIMPSbYhzxDD5UiYZxLpOb7oyd/1Hwa1B7ABtawYJYBx2U1sjYTMjv787C7/AP8RWuTyWRNgnAm8PPtyjNAHWZNgnAmUAas8Xi8JkE4E6hhrZBARbBuOjs7nRu2J0sQCP1NWgTOVhKs0WhEu1aHFR8EIMCFDGs0GpUBazgc0q7VYSUF6RzAwWWmCJxtOBwGsAzTxjDHcV/ffs9mTsQ/OCHkRPU6MG+eZ9Ug4VDkRDVtWVxEBLHr+ZsiAQAgUq/td66NyOgf24P0WQSwdMPCMMdxn19e7ZkT8asSIVLfuG4REGRLNyxdbgKR+nmCWLIIghz8SF+gSgR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pdlf8OeHdWw6ph1bAqt39WIZ/Z8vdBLQAAAABJRU5ErkJggg==" alt="" />
ok!完成了上面提出的需求
但是有一点不足的是如果查询的深度很深,代码就会很长,像上面的书写代码的方式,不易于我们阅读,有如下代码:
select *
from Customers
Where Id IN(select CustomerId
from Orders
where Id IN(select orderId
from OrderItems
where ProductId=1))
这段代码层次分明,比之前的代码更加的容易理解!
3、下面是使用子查询必须知道的几点:
(1)很重要的一点,作为子查询的SELECT语句只能查询单个列。企图检索多个列将返回错误(上面列子中的第(2)步已给出证明)。
(2)子查询的性能:上面给出的列子中的代码有效,并且获得了所需的结果。但是使用子查询并不总是执行这类数据检索最有效的方法。
所以,这里我使用内联结的方式完成上面所提出的需求,代码如下:
select * from
dbo.Customers a INNER JOIN
dbo.Orders b ON a.Id=b.CustomerId INNER JOIN
dbo.OrderItems c ON c.OrderId=b.Id
where c.ProductId=1
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdAAAAAoCAIAAADBiTyXAAAJxklEQVR4nO1du27bOhjmixTtE3gKAjZv0KFDk7VZ5MBrhyCdujRbbRdIgDxDloNTyz6AnyFDgADOZQlwer+cqY4bX2PDZ9CNIn9SlCxaNE3iRyBT1H/99Pm3rFhoPp//9fc/c5Wj1+tpoqRA68X6r2KYF5EZw1Ss6ulV4uj1eue/LkJBc/WEe3d3p4mSAq0X67+KYV5EZgxTsaqnV4nj7u7OEm4B1lcULoJhXkRmDFOxqqdXiUMPwr092to6uhXPyChBxDFJGhYZi4cAwuX2aAsFo9LOxdOUI30hwsE9AdoVhCAVAltkIopKhSlDEVbn7UpQHyE+ZPCTCXUcBsgEm2xckdXtZRNut9ulp9K7Dishq6+ScBcPgdUQf79oV2QQk3uMCxAukJP5fD6ftyuoUqkAOsSEG+26PdripEJliY0ZKrAagyfV6CTaklkjcVRCXKmwIb9YbELO7WUT7u/fv+mp9K7zlBxVAiCoPBsXD4HRkMldnQgXyIl/fKU9bwOMK0u4fB8s4UqMZWC1XeF2B8o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vwtonOMs77/rcAJkFws0c5p0+bqsKlblO1x9CFfU4ZpBuOf8a7gsF8vTsakgXkTEEZF8J9vhxknHZ8l4R+lvM1d1w8WsdVI4X4XFeT9oSFlW5exKqF14ZYPHqqAVjuZlIE1PrBrX4cIPfSB+FD2rFHJJId8LC6aCWFFOaL6Tu4bLIWXgcD6DQ9Yj4TFd6Eb4pRD7AZ+7C+qF6Wx4F7JlDkkMTWxLXBc658o0qBDTruGuKOFGkQhvC1u82EaCWElOTneAL/3puxTYqGOsF6NF9nD+Z3DWenTxlHWMn3xJTmQiYoxG91FwA4QWg5phW7J14QebuwYVIt/hrsZdCpyn7LiO/0ST7GIJV1sQq8hJdBss0Qnmfh8uR2GCdfn7cOEvzehdse6bWhwajS2O+Zy0mL3Uy78bLLEugmBz16BCTLsPV93jK5bzn2YIui0sL8419b93bETmialYTepwdRRRh6vu8RWqCZdk2HPpr8vIsbYgthGZJ6Zi1bTfUlD3+Ar74zWF+69nTqyoEFOxalqHq+4JE5ZwC/dfz5xYUSGmYtW0DlfdQyYs4Rbuv545saJCTMWqaR2uHXbYYYfOAxX9vJy0f4G3DY9wC3/7smLFihXjxRKuFStWrCxJLOFasWLFypLkf92giLL0dur1AAAAAElFTkSuQmCC" alt="" />
这里我的表数据有点变动,所以只看代码的逻辑即可!
4、下面改变当前随笔2中的需求,需要显示Customers表中的每个顾客的订单总数。
简单分析下思路,因为Customers表中没有订单总数这个字段,所以目前我们只能用一个计算字段来代替订单总数,而这个计算字段的列值必须是每个顾客的订单总数。
(1)先从Customers表中检索出顾客列表Id
(2)对于检索出来的每个顾客,统计其在Orders表中的订单数目
一般情况下统计一个顾客在Orders(订单表)中的订单总数,可以这样做:
select count(*) from Orders where CustomerId=1
上面代码统计了顾客编号为1的订单总数,但是这里需要统计的是所有用户的订单总数,显然这样写不对,我们可以这样写:
select name,(select COUNT(*) from Orders where Orders.CustomerId=Customers.Id) as orders from Customers
这段代码实际并不能检索出来结果,因为他将一个计算字段,被插入到其他的SELECT语句中一起被返回,该查询对对检索出的每个顾客ID执行一次 select COUNT(*) from Orders where dbo.Orders.CustomerId=当前顾客Id的运算;因为有5个顾客,所以会进行5次运算。返回每个顾客的总订单数。
下面是完整代码:
select name,(select COUNT(*) from Orders where Orders.CustomerId=Customers.Id) as orders from Customers
子查询中的WHERE子句(where Orders.CustomerId=Customers.Id)与前面使用的WHERE子句稍有不同,因为它使用了完全限定列名,而不只是列名。他指定列名和表名(Orders.CustomerId和Customers.Id),这个Where子句等于告诉SQL比较Orders的CustomerId和当前正从Customers表中检索的Id
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHoAAABuCAIAAABnfJA+AAAGs0lEQVR4nO2dW27bOBSGz6IGBeNlzOzANryBWcE8VpIBF+guBtPKGSCrCFDAtxcD06ZN08yTL01sR5bhPuhG3SmRh6JiHhAFI9E85OdjieRfSnA+n//+598zpm23W9T6VfYe2na7vX38BGd83JvNBrV+lb2HttlsNG55pnEX2nLU6YyWQpshCfd6vUatH8W7aNzr9VoS7tVqhVo/infRuFerVYT7eHT3Bwcjue7px+P/sfrnQ0KGZg88642d/cGxgz/9I/MhIf0eAQAg1rXpZxb+x/2CV+a8uvdYDX37QPvq29EpADKcpd1RhU3ryi/oNaysGRFu5+ju9i8YyXVP3x9+xOqfWQSgawd5Yk3pj3hHZhYBYsy8wkGGWNP9wiDEmOV8lsX7fmEQ3/vUJNAbRy6oUzu7D5nuosLjLvRtvzt9m6EZMnCfTqf77w9J3CGmMD+zqLCypvRxOkMXAwAIWPB4T/sqcGeHZRaG95szF4wQKNzO8Xl3wEju6fTt/iFW/9QixJrE8uMuEGNKHQnLJDJTi0Dvo1jvaV8F7ugyu8Pz7vCxC1DWJK8ZSuEOWmz3oAD3bm4Q6NrBZ9n6Ge9dVMPEINAdU+2Jn8p0R7fHD5G5QYJYYcT94hyfnvcYyXVPd1/vY/VPzDfEnMTzE4P61RJzEpZJZ57tblDu7aS6d6+q0FG6Pf4pku2OKhy2+Y0xY2lGh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F7mXRHxzLPlqAOIuNPzw0z0OLiVE8+Wo05nMOB9+Veli0k4dxfe1Zykjng27kPvemZdlb7Ti1M8K8CNv2aiiprjP0FwahKW90zVUHPy2AkM7dbgDinj4aa5F/yZPljp+1AVd+pNVbQhiWfpK3XetZvlK2kvbmFCJSPu8GIifFCoKO68155NDIInnjEifoW4GxHPWAYYBWdbrOY0Ip4loKcHKrW/CdVxa/FMKm4tnknFrcUzqbi1oVoSN88iFOcS1SVGt8atcWvcCLgZV1brrRS2BXf01EZOPSevw1Wnizi4VRHPBLx+qxj3bc5qiSzcSolnvPvNmHDnoXz90Z0Uz+bDcMUbWzxjx81yiW8H7qR4ZveDJwgiboUqRVkVbmtxZwlpqBv90l9A6ZE2425o51kexMvCTe08G3eBGFMZ4lnxSverwi1/51kxPhbcLb5VNr7zrCHcMlIW7uYe25ggWDAIQRgINoRbi2cS2vALWs0GmzQrMwAAAAAASUVORK5CYII=" alt="" />
SQL学习之学会使用子查询的更多相关文章
- SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
原文:SQL点滴10-使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比 今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章 ...
- 你真的会玩SQL吗?无处不在的子查询
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- paip.sql索引优化----join 代替子查询法
paip.sql索引优化----join 代替子查询法 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...
- 使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...
- 走向面试之数据库基础:二、SQL进阶之case、子查询、分页、join与视图
一.CASE的两种用法 1.1 等值判断->相当于switch case (1)具体用法模板: CASE expression WHEN value1 THEN returnvalue1 WHE ...
- SQL主外键和子查询
主键 数据库主键是指表中一个列或列的组合,其值能唯一地标识表中的每一行.这样的一列或多列称为表的主键,通过它可强制表的实体完整性.当创建或更改表时可通过定义 PRIMARY KEY约束来创建主键.一个 ...
- SQL笔记-第八章,子查询
一.SELECT列表中的标量子查询 查询每种书籍类型中的最早出版的书籍.在SQL 查询中,需要将一本书籍的出版年份与该类型的所有书籍的出版年份进行比较,并且仅仅在它们匹配时,才返回一个记录 SELEC ...
- SQL Server高级内容之子查询和表链接
1.子查询概念 (1)就是在查询的where子句中的判断依据是另一个查询的结果,如此就构成了一个外部的查询和一个内部的查询,这个内部的查询就是自查询. (2)自查询的分类 1)独立子查询 ->独 ...
- [SQL SERVER系列]之嵌套子查询和相关子查询
子查询有两种类型,一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数:另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的 ...
随机推荐
- rootvg 镜像
具体操作步骤如下 : a) 查看一下当前可用的硬盘: # lspv hdisk0 0002d74f0e69d97a rootvg ...
- UIView 属性
1.alpha 设置视图的透明度.默认为1. ojbc // 完全透明 view.alpha = 0; // 不透明 view.alpha = 1; 2.clipsToBounds 默认是NO,当设置 ...
- Weblogic 部署注意事项
Weblogic下部署服务注意事项: 1. 解决weblogic与hibernate的jar包冲突问题: 首 先找到该系统的DOMAIN_HOME(即你所建的域所在的位置)如:域empi的DOMAIN ...
- QF——OC中的SEL类型和Block
@selector(): 可以理解@selector()就是取类方法的编号,他的基本行为类似于C语言中的函数指针(指向函数的指针).它们通过传递方法的地址(或编号)来实现把方法当做参数的效果. 不过在 ...
- Android SwipeRefreshLayout
首先介绍一下 SwipeRefreshLayout ,由于下拉刷新使用的人比较多,于是谷歌自己就做了一个下拉刷新的控件. android.support.v4.widget.SwipeRefreshL ...
- Android Material Design调色板
转: http://www.stormzhang.com/design/2014/12/26/material-design-palette/ Material Design出来一段时间了,身为And ...
- HTML5学习笔记之Input类型
Input类型——email email类型用于包含email地址的输入域,在输入地址时会自动验证email域的值 例:Email:<input type="email" n ...
- Oracle EBS-SQL (GL-1):从总帐追溯到接收
SELECT je_header_id, je_line_num, trx_class_name, trx_type_name, trx_number_displayed, trx_date,comm ...
- C# 常用函数和方法集
1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System. ...
- DMVsinSQLServer -- 备
/*************************************************************************************************** ...