查询定购了23号产品20件以上的顾客的列表
sql语句1:
use northwind
select orderid,customid
from orders AS or1
where 20<(select quantity
from [order details] AS od
where or1.orderid=od.orderid
and od.productid=23)
这是相关子查询,相关子查询的执行方式是针对外层的每一条记录执行里层的查询,我写了另外两条sql语句:
use northwind
1.select quantity, od.orderid into _temp
from [order details] as od
where od.orderid in (select orderid from orders)
and od.productid=23
2. select orderid,customerid
from orders
where 20<(select quantity from _temp
where orders.orderid=_temp.orderid)
我执行了下,结果是一样的,但是我不知道这样改是否是等价的,想证明下,望高手赐教啊:)
sql语句1:
use northwind
select orderid,customid
from orders AS or1
where 20<(select quantity
from [order details] AS od
where or1.orderid=od.orderid
and od.productid=23)
这是相关子查询,相关子查询的执行方式是针对外层的每一条记录执行里层的查询,我写了另外两条sql语句:
use northwind
1.select quantity, od.orderid into _temp
from [order details] as od
where od.orderid in (select orderid from orders)
and od.productid=23
2. select orderid,customerid
from orders
where 20<(select quantity from _temp
where orders.orderid=_temp.orderid)
我执行了下,结果是一样的,但是我不知道这样改是否是等价的,想证明下,望高手赐教啊:)
解决方案 »
- BBS sql 问题请大侠指点!!
- 我想查登录sql2005的用户是否在线登录
- 请教各位大侠,这个Sql怎么写?谢谢啊!!!
- 表A,字段如下(RecordID,DoorList),其中DoorList,为nvarchar,值为:'1,3,4',有一变量S='3,4',显示DoorList中含有3,或4的RecordID记录.
- SQL SERVER智能查询问题
- 数据库连接问题!!
- 如何知道当前表名?
- 查询重复数据问题。
- SQL数据库里的游标如何来控制?如何达到条件时退出游标,这个条件如何来写?
- 批查询返回结果的顺序问题
- 如何将某数据库里存在的所有表名查找出来
- 请问如何按门禁系统里的刷卡记录统计加班时间
如果
orders:[order details] === 1:N
子查询会出错
所以,这却语句应该是
select orderid,customid
from orders AS or1
where exists (select 1
from [order details] AS od
where or1.orderid=od.orderid
and od.productid=23
and od.quantity>20
)