首页 > 解决方案 > 如何将 select 中的计算用于另一个计算

问题描述

我正在尝试使用从一个选择到另一个选择的命名计算,使用 Ver 15.1 Distrib 10.1.38-MariaDB,对于使用 readline 5.2 的 debian-linux-gnu (x86_64)。

select f.price * t.price as result_price, result_price-30/30 as percentage from table f join table t on f.to_id = t.from_id where f.from_id = 12 and t.to_id=205;

它给了我一个错误“错误1054(42S22):'字段列表'中的未知列'result_price'”。我怎样才能让它在 select 语句中看到 result_price,或者我只是简单地复制并写“(t.price*f.price - 30) / 30”。哪个效率更高?

标签: mysqlmariadb

解决方案


您可以将变量分配给计算,然后在查询中重用它。

SELECT @result_price:=f.price*f.price AS result_price, (@result_price - 30)/30 AS percentage FROM ...

推荐阅读