我在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;发布于 2021-05-03 17:00:13
派生表是实现这一点的一种方法,但您的别名是反向定义的,因为别名是在外部而不是内部定义的。试试这个版本:
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;https://stackoverflow.com/questions/67366066
复制相似问题