X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id EC9379DC9C8; Mon, 20 Mar 2006 23:33:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 51863-04; Mon, 20 Mar 2006 23:33:29 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from relais.videotron.ca (relais.videotron.ca [24.201.245.36]) by postgresql.org (Postfix) with ESMTP id 6D1489DC8E1; Mon, 20 Mar 2006 23:33:21 -0400 (AST) Received: from lythanhphu ([66.130.233.129]) by VL-MH-MR002.ip.videotron.ca (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTP id <0IWG005CZKJNGPE0@VL-MH-MR002.ip.videotron.ca>; Mon, 20 Mar 2006 22:33:23 -0500 (EST) Date: Mon, 20 Mar 2006 22:33:22 -0500 From: Daniel CAUNE Subject: Re: Update counter when row SELECT'd ... ? In-reply-to: <20060320230344.Y1178@ganymede.hub.org> To: "'Marc G. Fournier'" , pgsql-sql@postgresql.org Message-id: <0IWG005D0KJNGPE0@VL-MH-MR002.ip.videotron.ca> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Mailer: Microsoft Office Outlook, Build 11.0.6353 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 7BIT Thread-index: AcZMlKfBDpXToq7gQPeS2mOk9bDGWwAAl2Lg X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.55 required=5 tests=[AWL=0.550, UPPERCASE_25_50=0] X-Spam-Score: 0.55 X-Spam-Level: X-Archive-Number: 200603/321 X-Sequence-Number: 24667 > I have a simple table: > > name, url, counter > > I want to be able to do: > > SELECT * FROM table ORDER BY counter limit 5; > > But, I want counter to be incremented by 1 *if* the row is included in > that 5 ... so that those 5 basically move to the bottom of the list, and > the next 5 come up ... > > I've checked CREATE TRIGGER, and that doesn't work 'ON SELECT' ... is > there anything that I *can* do, other then fire back an UPDATE based on > the records I've received? > > Thanks ... > You mean that you want to write a SELECT statement that returns the 5 first rows that have the smallest counter, and just after what, you would like to increment their counter, right? I was thinking of using a table function, something like (I didn't test it): CREATE OR REPLACE FUNCTION my_first_url(P_Limit IN int) RETURNS SETOF table AS $$ BEGIN FOR V_Record IN SELECT * FROM table ORDER BY counter LIMIT P_Limit LOOP UPDATE table SET counter = counter + 1 WHERE name = V_Record.name /* AND url = V_Record.url */; -- if needed RETURN V_Record; END LOOP; END; $$ LANGUAGE PLPGSQL; -- Daniel