Unix Technical Forum

Re: mysql question : CASE

This is a discussion on Re: mysql question : CASE within the MySQL forums, part of the Database Server Software category; --> You might want to try comp.databases.mysql for comprehensive answers. (X-Post and Followup-To set) Geradeaus wrote: > I have searched ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:15 AM
maraguida@gmail.com
 
Posts: n/a
Default Re: mysql question : CASE

You might want to try comp.databases.mysql for comprehensive answers.
(X-Post and Followup-To set)

Geradeaus wrote:
> I have searched but have not yet found how I can resolve the following
> problem :
>
> SELECT adId,
> CASE
> WHEN adId =1 THEN cityId
> WHEN adId =2 THEN regionId
> END AS extraField
> FROM ad
> WHERE extraField <> 0
>
> The problem is "extraField" cannot be used in the WHERE clause... I am using
> mySQL 4.1


....
WHERE adId in (1, 2)

not tested!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:15 AM
Bill Karwin
 
Posts: n/a
Default Re: mysql question : CASE

> Geradeaus wrote:
>> I have searched but have not yet found how I can resolve the following
>> problem :
>>
>> SELECT adId,
>> CASE
>> WHEN adId =1 THEN cityId
>> WHEN adId =2 THEN regionId
>> END AS extraField
>> FROM ad
>> WHERE extraField <> 0
>>
>> The problem is "extraField" cannot be used in the WHERE clause... I am
>> using
>> mySQL 4.1


This is a documented limitation in MySQL, and in the SQL language.

Excerpts from http://dev.mysql.com/doc/refman/5.0/en/select.html :

A select_expr can be given an alias using AS alias_name.
The alias is used as the expression's column name and can be used
in GROUP BY, ORDER BY, or HAVING clauses.
. . .
It is not allowable to use a column alias in a WHERE clause, because
the column value might not yet be determined when the WHERE clause
is executed.

Example of the latter case:

SELECT SUM(lineitem_value) AS order_subtotal
FROM order_lineitems
WHERE order_subtotal > 999
GROUP BY order_id;

See also http://dev.mysql.com/doc/refman/5.0/...ith-alias.html

Regards,
Bill K.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:25 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com