sqlserver 子查询

本文介绍了使用SQL进行复杂查询的方法,包括比对特定产品价格更贵的产品、找出销售超过50次的产品、查询未曾下单的客户及实现分页查询等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--产品id  产品名 单价
select productid,productname,unitprice from products--产品表
--定单编号  产品id  单价  数量
select orderid,productid,unitprice,quantity from [order details]--定单详细表
--定章编号   客户id  货运方式   定单日期
select orderid,customerid,shipname,orderdate from orders--定单表

--1.查找比某一产品贵的产品,并显示产品的所有信息?
--方法1>编程方式
declare @price money
select @price=unitprice from products where productname='Chang'
select  productid,productname,unitprice from products where unitprice>@price
--方法2>一般子查询,主要应用子查询的结果做为其它sql语句的条件
--以下查询,返回一个unitprice列所对应的值
--select unitprice from products where productname='Chang'
select  productid,productname,unitprice from products 
where unitprice>(
select unitprice from products where productname='Chang')

update products set unitprice=unitprice*10
where unitprice>(
select unitprice from products where productname='Chang'
)

--2.查询出销售过50次及以上的产品
--[order details]表中可以得出每件产品销售的次数
--products:表中得到产品信息
--方式1>关连查询
--1.1:关连
select p.productid,p.productname,p.unitprice,o.orderid from products p
inner join [order details] o on p.productid=o.productid
--1.2:分组 count(*):统计符合条件的记录数;分组之后功能为统计组内成员数
select p.productid,p.productname,count(*)
 from products p
inner join [order details] o on p.productid=o.productid
group by p.productid,p.productname
having count(*)>50
--方式2>in子查询
--查询结果单列多行,相当于一个集合
--select productid from [order details] group by productid having count(*)>50
--in 对应集合查询
--select productid,productname,unitprice from products
--where productid in (1,4,5)

select productid,productname,unitprice from products
where productid in (
select productid from [order details] group by productid having count(*)>50
)

--3.查询没有下过定单的客户
--not in子查询
select * from customers 
select customerid from orders group by customerid --已下单的客户
select * from customers where customerid not in
(select customerid from orders group by customerid)

--4.应用:分页查询
declare @pageSize int --每页显示多少条记录
declare @pageNo int --第几页
set @pageSize=5
set @pageNo=2
select top (@pageSize) productid,productname,unitprice from products
where productid not in
(select top ((@pageNo-1)*@pageSize) productid from products)

 

转载于:https://www.cnblogs.com/kite/p/3635579.html

### SQL Server 子查询使用方法 子查询是在另一个查询内部嵌套的查询语句。外部查询称为父查询,而内部查询则被称为子查询子查询可以返回单个值、一列或多行多列的结果集。 #### 单行子查询子查询只返回一行数据时,通常用于比较运算符(=, <>, >, >=, <, <=)。例如: ```sql SELECT EmployeeID, FirstName, LastName FROM Employees WHERE DepartmentID = ( SELECT DepartmentID FROM Departments WHERE DepartmentName = 'Sales' ); ``` 此查询会获取属于销售部门的所有员工的信息[^1]。 #### 多行子查询 如果子查询可能返回多个值,则应配合 `IN` 或者集合操作符 (`ANY`, `ALL`) 来处理这些情况。比如查找工资高于平均薪资水平的员工: ```sql SELECT EmployeeID, FirstName, LastName, Salary FROM Employees WHERE Salary > ALL( SELECT AVG(Salary) FROM Employees GROUP BY DepartmentID ); ``` 这段代码展示了如何找出那些薪水超过其所在部门平均水平的所有雇员[^2]。 #### 关联子查询 关联子查询是指在子查询中引用了来自外层查询表中的字段的情况。这类查询对于每一条记录都会重新计算一次子查询部分。下面是一个例子,它显示了每个订单及其对应的客户名称: ```sql SELECT o.OrderID, c.CustomerName FROM Orders o JOIN Customers c ON o.CustomerID = c.CustomerID; ``` 实际上这里并没有真正意义上的子查询;但是为了说明概念上的区别,在某些情况下确实存在这样的需求——即在一个更复杂的场景下通过子查询方式实现相同的功能[^3]。 #### 使用 WITH 语法简化复杂查询 有时为了让逻辑更加清晰或者提高性能,可以通过公共表达式 (Common Table Expression, CTE),也就是WITH关键字定义临时结果集来进行进一步的操作。这有助于分解大型查询使之易于理解和维护: ```sql WITH HighPaidEmployees AS ( SELECT EmployeeID, FirstName, LastName, Salary FROM Employees WHERE Salary > ( SELECT AVG(Salary) FROM Employees ) ) SELECT * FROM HighPaidEmployees; ``` 上述脚本先创建了一个名为HighPaidEmployees的CTE来存储高薪员工的数据,然后再从中选取全部条目作为最终输出[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值