0

I m try to do something like that:

Select column_name*other_column_name as sqr 
From table 
Where sqr<25 
Order by(sqr)

column_name and column_name are columns of my database where I store int values.

I find that

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined. For example, the following query is illegal.

does anybody Know how to get through in a polite way, please don't say that:

Select column_name*other_column_name as sqr 
From table 
Where column_name*other_column_name<25 
Order by (column_name*other_column_name); 

because in this example I simplify the equations but in my project the equation is really long

thanks sorry for my english

2 Answers 2

5

You can just wrap your query in a subquery and then you can use the alias:

SELECT *
FROM
(
    Select column_name*other_column_name as sqr
    from table 
) X
where x.sqr < 25 
order by x.sqr;
1
  • This is the way I'd have gone as well. Jul 25, 2012 at 17:15
1

Will this work?

SELECT (n1 * n2) as `sqr` FROM temptable HAVING `sqr` < 25 ORDER BY `sqr`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.