Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id E9775633838 for ; Wed, 12 Aug 2009 15:52:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 52025-03 for ; Wed, 12 Aug 2009 18:52:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by mail.postgresql.org (Postfix) with ESMTP id 57B366325A9 for ; Wed, 12 Aug 2009 15:52:41 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1MbIwA-0001xh-5t for pgsql-sql@postgresql.org; Wed, 12 Aug 2009 18:52:38 +0000 Received: from ppp-88-217-23-240.dynamic.mnet-online.de ([88.217.23.240]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 12 Aug 2009 18:52:38 +0000 Received: from spam_eater by ppp-88-217-23-240.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 12 Aug 2009 18:52:38 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: mysql code questions Date: Wed, 12 Aug 2009 20:52:33 +0200 Lines: 22 Message-ID: References: <20090812181928.GF7527@cns.vt.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ppp-88-217-23-240.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: <20090812181928.GF7527@cns.vt.edu> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RCVD_IN_DNSWL_LOW=-1 X-Spam-Level: X-Archive-Number: 200908/59 X-Sequence-Number: 32968 Ray Stell wrote on 12.08.2009 20:19: > http://www.brentozar.com/archive/2009/04/getting-the-most-recent-record/ > How this works? What is ttNewer? What is a clustered primary key in mysql? That article talks about SQL Server not MySQL. > select tt.* FROM TestTable tt > LEFT OUTER JOIN TestTable ttNewer > ON tt.id = ttNewer.id AND tt.create_date < ttNewer.create_date > WHERE ttNewer.id IS NULL; I would probably do it this way: SELECT tt. * FROM testtable tt WHERE create_date = (SELECT MAX(create_date) FROM testtable tt2 WHERE tt.id = tt2.id); Don't know which one is more efficient (with just a few rows, it doesn't really pay off to look at the execution plan) Thomas