przemo_li
przemo_li

Reputation: 4053

MySQL how to reuse select column aliase, when I aliase subquery?

I have something like:

Select1 ... 
        ,(fieldA - fieldB) AS Y, 
        ,(Select2 
                 (Select3 percent 
                    From ... 
                    Join.... 
                    Where ... 
                    Order by some_date 
                    Limit 1) AS X 
            From ... 
            Join... 
            Where ... 
            Order by some_date2 
            Limit 1) AS X
        ,(X * Y) AS output
  From ...
  Join ...
  Join ...

Everything is ok till I need to reuse value returned by that nested select.

How do I reuse X alias in definition of "output" column?

PS Wont past code as its more then A4 right now! It works as expected but is hard on eyes.

Upvotes: 0

Views: 344

Answers (1)

Biscuits
Biscuits

Reputation: 1807

You can do this by using, once again, another nested SELECT. This new outer-most SELECT can then access X and Y.

Select0
        ...
        ,(X * Y) AS output
    From (
        Select1 ... 
            ,(fieldA - fieldB) AS Y, 
            ,(Select2 
                     (Select3 percent 
                        From ... 
                        Join.... 
                        Where ... 
                        Order by some_date 
                        Limit 1) AS X 
                From ... 
                Join... 
                Where ... 
                Order by some_date2 
                Limit 1) AS X
      From ...
      Join ...
      Join ...) AS values

Upvotes: 1

Related Questions