使用连接而不是子查询

时间:2014-10-07 20:22:16

标签: postgresql join subquery

对于这个问题:    通过在京都至少为客户订购一个订单的代理商订购产品。这次使用加入;没有子查询。

我能够使用子查询得到答案:

select distinct pid
from orders
where aid in (
     select aid 
     from orders
     where cid in(
         select cid
         from customers
         where city = 'Kyoto'
                 ) 
              )

我无法弄清楚如何仅使用连接来做到这一点。 此代码返回了我需要获取pid的帮助,但是我无法想出一种方法来获取它们而不使用子查询:

select distinct o.aid
from orders o, customers c
where o.cid = c.cid
and c.city = 'Kyoto'

以下是我正在使用的两个表:

客户:

 cid name city discount
 c001 Tiptop Duluth 10.00
 c002 Basics Dallas 12.00
 c003 Allied Dallas 8.00
 c004 ACME Duluth 8.00
 c005 Weyland-Yutani Acheron 0.00
 c006 ACME Kyoto 0.00   

和订单:

 ordno mon cid aid pid qty dollars
 1011 jan c001 a01 p01 1000 450.00
 1013 jan c002 a03 p03 1000 880.00
 1015 jan c003 a03 p05 1200 1104.00
 1016 jan c006 a01 p01 1000 500.00
 1017 feb c001 a06 p03 600 540.00
 1018 feb c001 a03 p04 600 540.00
 1019 feb c001 a02 p02 400 180.00
 1020 feb c006 a03 p07 600 600.00
 1021 feb c004 a06 p01 1000 460.00
 1022 mar c001 a05 p06 400 720.00
 1023 mar c001 a04 p05 500 450.00
 1024 mar c006 a06 p01 800 400.00
 1025 apr c001 a05 p07 800 720.00
 1026 may c002 a05 p03 800 740.00

0 个答案:

没有答案