Xenione
Xenione

Reputation: 2213

MYSQL database select issue

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

Upvotes: 0

Views: 92

Answers (2)

Layton Everson
Layton Everson

Reputation: 1148

Will this work?

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

Upvotes: 1

Taryn
Taryn

Reputation: 247690

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;

Upvotes: 5

Related Questions