Received: from mail.zhengmai.net.cn ([202.99.6.10]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5R3OOa88729 for ; Tue, 26 Jun 2001 23:24:24 -0400 (EDT) (envelope-from laser@zhengmai.com.cn) Received: from zhengmai.com.cn ([61.135.89.142]) by mail.zhengmai.net.cn (8.9.3/8.9.3) with ESMTP id LAA27435; Wed, 27 Jun 2001 11:23:17 +0800 (BST) Message-ID: <3B3952DB.A665D28E@zhengmai.com.cn> Date: Wed, 27 Jun 2001 11:28:28 +0800 From: He Weiping X-Mailer: Mozilla 4.76 [en] (X11; U; Linux 2.2.19 i686) X-Accept-Language: en MIME-Version: 1.0 To: Tom Ivar Helbekkmo CC: Tom Lane , pgsql-docs@postgresql.org Subject: Re: doc question about column name alias References: <3B374CF6.C1AB82F0@zhengmai.com.cn> <15864.993483354@sss.pgh.pa.us> <867kxzjmat.fsf@athene.i.eunet.no> Content-Type: text/plain; charset=gb2312 Content-Transfer-Encoding: 7bit X-Archive-Number: 200106/43 X-Sequence-Number: 841 Tom Ivar Helbekkmo wrote: > Tom Lane writes: > > >> SELECT a AS b FROM table1 ORDER BY a; > >> I think it should be: > >> SELECT a AS b FROM table1 ORDER BY b; > > > > You are correct that the latter is the SQL-standard version. > > In fact, this becomes pretty obvious when you consider the theoretical > ordering of the evaluation of the various parts of the statement. The > ORDER BY is executed *after* the SELECT part, so it only knows about > the column name 'b', and doesn't know what 'a' refers to. Actually, > ORDER BY isn't even part of the SELECT statement, really, but hides an > implicit CURSOR operation taking place during data output. Allowing > the first version to work is of questionable value, since it might > cause errors and confusion when code is modified. Consider the case > where you have "SELECT a AS b, b AS a ...". I've done it! :-) > > The order of evaluation is: FROM, WHERE, GROUP BY, HAVING, SELECT, > and, finally, ORDER BY. Thanks all for these info. & examples, they help me much to clearify the question, the present wording is a little bit hard to understand, but accurate and efficent, and I think it's better to add a few lines to make it clear, like "...but the SQL standard require (or "only permit") us to do: select a as b from table1 order by b;" regards laser