Re: using LIMIT only on primary table

From: "Dan Langille" <dan(at)langille(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: using LIMIT only on primary table
Date: 2002-03-02 23:40:19
Message-ID: 200203022340.g22NeNk56386@lists.unixathome.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

On 2 Mar 2002 at 18:22, Tom Lane wrote:

> "Dan Langille" <dan(at)langille(dot)org> writes:
> > 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
>
> I think you want
>
> SELECT *
> from
> (SELECT * from orders order by ID DESC limit 100) as recent_orders,
> order_items
> where order_items.order_id = recent_orders.id

That is it Tom. Thank you. As soon as I saw your solution, I thought of
creating a view to do this.

CREATE VIEW recent orders AS
SELECT * from orders
order by ID DESC
limit 100;

Which makes the query:

SELECT *
from recent_orders, order_items
where order_items.order_id = recent_orders.id

Cheers
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Anthony Estevez 2002-03-03 03:09:01 Duplicates Question.
Previous Message Tom Lane 2002-03-02 23:22:35 Re: using LIMIT only on primary table

Browse pgsql-sql by date

  From Date Subject
Next Message John Oakes 2002-03-03 00:28:23 Index doesn't appear to be working.
Previous Message Tom Lane 2002-03-02 23:22:35 Re: using LIMIT only on primary table