user225269 user225269 - 5 months ago 26
SQL Question

How to use alias as field in mysql

I'm trying to do something like this. But I get an unknown column error:

SELECT SUM(field1 + field2) AS col1, col1 + field3 AS col3 from core


Basically, I want to just use the alias so that I won't need to perform the operations performed earlier. Is this possible in mysql?

Answer

Consider using a subquery, like:

SELECT col1
,      col1 + field3 AS col3 
FROM   (
       SELECT  field1 + field2 as col1
       ,       field3
       from    core
       ) as SubQueryAlias
Comments