From: Junaili Lie <junaili@gmail.com>
To: pgsql-performance@postgresql.org
Subject: Help with rewriting query
Date: Wed, 8 Jun 2005 12:34:32 -0700
Message-ID: <8d04ce990506081234121995f9@mail.gmail.com> (raw)
Hi,
I have the following table:
person - primary key id, and some attributes
food - primary key id, foreign key p_id reference to table person.
table food store all the food that a person is eating. The more recent
food is indicated by the higher food.id.
I need to find what is the most recent food a person ate for every person.
The query:
select f.p_id, max(f.id) from person p, food f where p.id=f.p_id group
by f.p_id will work.
But I understand this is not the most efficient way. Is there another
way to rewrite this query? (maybe one that involves order by desc
limit 1)
Thank you in advance.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: junaili@gmail.com
Subject: Re: Help with rewriting query
In-Reply-To: <8d04ce990506081234121995f9@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox