views:

34

answers:

1

hi

i am trying to run a query like this

SELECT a, b , c, (SELECT INNULL(x,y)) as mycol WHERE mycol < 400 ;

BUt it gives the error

#1054 - Unknown column 'mycol' in 'where clause'

What would be the right way to do this?

Thanks.

+2  A: 

It's so in MS SQL, so I assume, that the same problem is in MySQL.

Try to change WHERE to HAVING. WHERE clause doesn't see your renamed columns.

HAVING is working the same way as WHERE, like (mycol < 400 AND a > 5).

GROUP BY should be before HAVING.

Check the examples in the link.

http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm

hgulyan
thanks but sorry i forgot to mention that i am also using a group by clause in this query which seems to break the having thing. And what would be the syntax ifthere are multiple conditions to be checked with having ?
naiquevin
I've edited my answer. If it's what you wanted, just mark it as answer.
hgulyan
thanks this worked in a small sample query, will check if it does in the bigger one in a while. Thanks a lot.
naiquevin
and don't forget to mark it as answer. Do this in your other questions, if you got the answer.
hgulyan
ok will do it but a bit confused . i clicked on the green tick icon alongside this answer. I guess that does it.
naiquevin
Yeah, it is. Also you can vote up an answer (or anything else) if you find it useful. Read the faq:)http://stackoverflow.com/faq
hgulyan