Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f47Cr5A64911 for ; Mon, 7 May 2001 08:53:05 -0400 (EDT) (envelope-from pgsql-general-owner@postgresql.org) Received: from socrates.catalyst.net.nz (socrates.catalyst.net.nz [203.96.153.226]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f44Nw8A08040; Fri, 4 May 2001 19:58:08 -0400 (EDT) (envelope-from andrew@catalyst.net.nz) Received: from heidegger.catalyst.net.nz (hume.catalyst.net.nz [203.96.153.228]) by socrates.catalyst.net.nz (8.9.3/8.9.3/Debian 8.9.3-21) with ESMTP id LAA27866; Sat, 5 May 2001 11:57:02 +1200 X-Authentication-Warning: socrates.catalyst.net.nz: Host hume.catalyst.net.nz [203.96.153.228] claimed to be heidegger.catalyst.net.nz Received: from 127.0.0.1 (ident=andrew) by heidegger.catalyst.net.nz with esmtp (MasqMail 0.1.14) id 14vacq-5m8-00; Fri, 04 May 2001 20:07:44 +1200 Message-ID: <3AF26350.A9F7AAC0@catalyst.net.nz> Date: Fri, 04 May 2001 20:07:44 +1200 From: Andrew McMillan Organization: Catalyst IT Ltd X-Mailer: Mozilla 4.76 [en] (X11; U; Linux 2.2.18-kant i686) X-Accept-Language: en MIME-Version: 1.0 To: Philip Hallstrom CC: Chris Ryan , Jason , pgsql-php@postgresql.org, pgsql-novice@postgresql.org, pgsql-general@postgresql.org Subject: Re: [NOVICE] Re: psql with PHP question References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200105/297 X-Sequence-Number: 9041 Philip Hallstrom wrote: > > Also... is there any reason you don't just do: > > SELECT MAX(id) FROM article > > rather than what you have below? It would get you the same thing wouldn't > it and save a query... > > On Thu, 3 May 2001, Chris Ryan wrote: > > > Jason, > > > > Look into the pg_result() function. You would use it something like > > this: > > > > $q = pg_Exec("SELECT id FROM article WHERE id=(SELECT MAX(id) FROM > > article)"); > > $maxid = pg_result($q,0,0); # pg_result($result,$row,$column_num) > > echo "The highest id is ". $maxid[0]; > > > > Hope this helps. > > > > Chris Ryan > > chris@greatbridge.com > > > > Jason wrote: > > > > > > Hi, I want to try and optimize my calls to postgreSQL from PHP. If > > > I only need one field from a select statement, what should I use? I am > > > currently using: > > > $q = pg_Exec("SELECT id FROM article WHERE id=(SELECT MAX(id) > > > FROM article)"); Or even more efficiently: SELECT id FROM article ORDER BY id DESC LIMIT 1; Since that will use an index on article.id if article is a big table, has been vacuum analyzed, and has an index available. Regards, Andrew. -- _____________________________________________________________________ Andrew McMillan, e-mail: Andrew@catalyst.net.nz Catalyst IT Ltd, PO Box 10-225, Level 22, 105 The Terrace, Wellington Me: +64(21)635-694, Fax: +64(4)499-5596, Office: +64(4)499-2267xtn709