Received: from mps4.plala.or.jp (mpsb-nat02.plala.or.jp [202.212.114.145]) by postgresql.org (Postfix) with ESMTP id D33D9474E44 for ; Sun, 3 Mar 2002 00:58:22 -0500 (EST) Received: from vip333 ([218.47.225.135]) by mps4.plala.or.jp with ESMTP id <20020303055826.SEDV2673.mps4.plala.or.jp@vip333>; Sun, 3 Mar 2002 14:58:26 +0900 Date: Sun, 03 Mar 2002 15:00:01 +0900 From: Masaru Sugawara To: dan@langille.org Subject: Re: using LIMIT only on primary table Cc: pgsql-sql@postgresql.org In-Reply-To: <200203022235.g22MZYk55756@lists.unixathome.org> References: <200203022235.g22MZYk55756@lists.unixathome.org> Errors-To: rk73@sea.plala.or.jp Message-Id: <20020303141057.614C.RK73@sea.plala.or.jp> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Becky! ver. 2.00.08 X-Archive-Number: 200203/23 X-Sequence-Number: 6905 On Sat, 2 Mar 2002 17:35:31 -0500 "Dan Langille" wrote: > If I want the last 100 orders: > > SELECT * FROM orders LIMIT 100; > > If I want all the items on the last 100 orders, I'd start like this: > > SELECT * > from orders, order_items > where order_items.order_id = orders.id > LIMIT 100 > > But that will only give me the last 100 items, not 100 orders. > > What I really want is > > SELECT * > from orders, order_items > where order_items.order_id = orders.id > and exists > (SELECT * from orders order by ID DESC limit 100); This probably gives you all the items on the last 100 orders. select * from (select * from orders order by ID desc limit 10) as o inner join order_items as oi on (oi.order_id = o.order_id) ; > > But that gives me all orders, not just the first 100. > > Adding a LIMIT 100 to the above doesn't work either. It equates to the > first example. Regards, Masaru Sugawara