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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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! |
| ||||
| > 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. |
| Thread Tools | |
| Display Modes | |
|
|