MySQL 如何重用 select 列别名,当我为子查询设置别名时?

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

我有类似的东西:

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 ...

一切正常,直到我需要重用嵌套 select 返回的值。

如何在 "output" 列的定义中重复使用 X 别名?

PS 不会像现在的 A4 那样过去的代码!它按预期工作,但很难看。

您可以再次使用另一个嵌套 SELECT 来完成此操作。这个新的最外层 SELECT 然后可以访问 XY.

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