using LIMIT only on primary table

From: "Dan Langille" <dan(at)langille(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: using LIMIT only on primary table
Date: 2002-03-02 22:35:31
Message-ID: 200203022235.g22MZYk55756@lists.unixathome.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

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);

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.

Clues? cheers
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Doug Silver 2002-03-02 22:53:24 Re: Select statement question
Previous Message Salvatore Sferrazza 2002-03-02 22:35:00 Re: ERD or UML tool

Browse pgsql-sql by date

  From Date Subject
Next Message daq 2002-03-02 23:20:59 Re: [SQL] using LIMIT only on primary table
Previous Message Dalton Shane 2002-03-02 16:42:33 arrays in stored procedures