簡體   English   中英

MySQL 如何復用別名創建別名

[英]MySQL how to reuse Alias to create Alias

我在 MySQL 中遇到別名問題,我有一個數據庫調用“Table_Profit”,這是我的查詢,它工作正常:

$sql="SELECT *,
Profit*24 AS 'Daily_Profit',
Profit*30*24 AS 'Monthly_Profit',
from Table_Profit;

但是現在我想通過使用其他別名添加另一個別名(Total_Profit)並且它不起作用,我該如何解決這個問題?:

$sql="SELECT *,
Profit*24 AS Daily_Profit,
Profit*30*24 AS Monthly_Profit,
Daily_Profit*Monthly_Profit AS Total_Profit
from Table_Profit;

我嘗試使用派生表,但它不起作用。

$sql="SELECT *,
Profit*24 AS Daily_Profit,
Profit*30*24 AS Monthly_Profit,
from (
select 
  Daily_Profit*Monthly_Profit AS Total_Profit
from Table_Profit
)AS P;

派生表是執行此操作的一種方法,但您的方法倒退了,因為您在外部而不是內部定義別名。 試試這個版本:

SELECT *, Daily_Profit*Monthly_Profit AS Total_Profit
FROM
(
    SELECT *, Profit*24 AS Daily_Profit, Profit*30*24 AS Monthly_Profit
    FROM Table_Profit
) t;

但在這種情況下,我可能會避免使用子查詢,而只是重復少量邏輯:

SELECT *,
    Profit*24 AS Daily_Profit,
    Profit*30*24 AS Monthly_Profit,
    (Profit*24) * (Profit*30*24) AS Total_Profit
FROM Table_Profit;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM