创建嵌套查询以从初始查询中过滤

时间:2013-09-18 17:28:31

标签: sql sql-server sql-server-2008

我正在尝试创建嵌套查询,以便查询从发票项目中选择所有ordernumbers和orderid,然后选择子租用的项目。

select
   do.orderid, do.orderno, ot.masteritemid, ot.qty
from dealorder do
inner join ordertran ot on do.orderid=ot.orderid and ot.orderid='A00M5BGA'
where ot.vendorid<>''

Select orderno, orderid from invoiceitemview where invoiceno='T646692'

我尝试过内连接,但似乎没有用。第一个查询给了我6个正确的项目,但是如果我执行连接,它似乎是获取不属于该订单的项目。因此,如何创建嵌套查询以从第二个查询中获取所有项目,然后使用第一个查询进行过滤。

2 个答案:

答案 0 :(得分:0)

这听起来像你在找什么

select
   do.orderid, do.orderno, ot.masteritemid, ot.qty
from dealorder do
inner join ordertran ot on do.orderid=ot.orderid and ot.orderid='A00M5BGA'
inner join (
    Select orderno, orderid from invoiceitemview where invoiceno='T646692'
) tmp ON tmp.orderno=do.orderno AND tmp.orderid=do.orderid
where ot.vendorid<>''

答案 1 :(得分:0)

试试这个。

select do.orderid, do.orderno, ot.masteritemid, ot.qty 
from (Select orderno, orderid from invoiceitemview where invoiceno='T646692') inv
inner join dealorder do inv.orderid=do.orderid
inner join ordertran ot on do.orderid=ot.orderid and ot.orderid='A00M5BGA'
where ot.vendorid<>''