Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a table of object records (lb_objs) that are frequently accessed. I keep track of these accesses in another table, lb_trans. lb_trans has columns for the primary key of the object acted on (lb_trans.obj_id), a timestamp (lb_trans.instant), and an access type (lb_trans.action). I'm trying to craft a way to select a set of the objects (e.g. lb_objs.attrib = 1), and include in each returned row the transaction type from the most recent transaction on the object specified by that row. With two queries it's simple:
followed by, for each result,SELECT id, attrib FROM lb_objs WHERE attrib = 1
where ? is a placeholder set to lb_objs.id for the current lb_objs row.SELECT action FROM lb_trans WHERE obj_id = ? ORDER BY instant DESC LIMIT 1
However, I'd prefer to do it in one select, since I could reuse that for other similar queries more directly. I've though about using various joins and sub-selects, but I can't find a way to do it. Can anyone see a way to do this with just one select?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: (OT) Postgres Query
by Ovid (Cardinal) on May 12, 2001 at 02:08 UTC | |
|
Re: (OT) Postgres Query
by mr.nick (Chaplain) on May 12, 2001 at 02:30 UTC | |
by Anonymous Monk on May 12, 2001 at 03:06 UTC | |
|
Re: (OT) Postgres Query
by thraxil (Prior) on May 12, 2001 at 20:18 UTC |