mysql - 如何将 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”。哪个效率更高?
解决方案
您可以将变量分配给计算,然后在查询中重用它。
SELECT @result_price:=f.price*f.price AS result_price, (@result_price - 30)/30 AS percentage FROM ...
推荐阅读
- sql - 在另一个表中为外键创建新行
- java - 如何为 Windows 10 的 HDF5view 应用程序安装 java
- python - 为什么在 python 中以不同方式导入时 setattr 不起作用?
- visual-studio - VS2019 DataSet Designer Mangling INSERT 查询 - 删除 RETURNING 语句
- docker - 阻止 Docker 容器退出
- linux - 使用videobalance调整gstreamer管道中的对比度和亮度将相机流保存到文件
- javascript - 使用 react-redux 我正在尝试创建中间件来执行异步等待逻辑,以便能够使用 this.props.getUser 操作功能
- reactjs - Formik 表单未通过 onclick 更新
- c# - OnDelete 的 EF Core 注释
- python - 使用二分搜索的猜谜游戏