From: Dan Nelson Date: May 27 2003 3:03am Subject: Re: How to write this query pls? List-Archive: http://lists.mysql.com/mysql/140841 Message-Id: <20030527030301.GA30427@dan.emsphone.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii In the last episode (May 27), Aamir Baig said: > You would figure that an sql query like following would work (I > modified it slightly as it needs to take into account the type > column): > > select P.personid , > SUM(CASE WHEN PH.type = 'PHN' THEN 1 ELSE 0 END) AS PHNC > From Person P left outer join Phone PH > ON P.personid > Where PHNC = 0 group by Phone.personid; > > But it says: > ERROR 1054: Unknown column 'PHNC' in 'where clause' > > I know it's something trivial, can't seem to place the finger at it > though.. You can't use a column alias in the WHERE clause; you'll have to duplicate the SUM expression. See http://www.mysql.com/doc/en/Problems_with_alias.html -- Dan Nelson dnelson@stripped