0

I have a query like this:

select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f from test;

Is there a way where I can use my previously defined alias as column so it will be like:

(a+b)/(a-b) as f from test to c/d as f from test

Or any other solution where I will not re-code any of the expression in this query.

Vainglory07
  • 5,073
  • 10
  • 43
  • 77

3 Answers3

2
select *
from (
 select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f 
 from test
) s
Eduard Uta
  • 2,477
  • 5
  • 26
  • 36
2

Use User-Defined Variables.

SELECT a,b, 
    @c := (a+b) as c, 
    @d := (a-b) as d, 
    @c*-1 as e, 
    @c/@d as f
FROM test;
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
1

Their can be many ways, but as per i know, you can do this by using subquery like this. you will get all your alias as column when you make main query as subquery and use it as per your requirement.

Check this tutorial for more stuff : http://www.mysqltutorial.org/mysql-subquery/

select a,b,c,d,e,f form 
 (select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f from test)
x;
bharatpatel
  • 1,203
  • 11
  • 22