Using an Alias in SQL Calculations(在 SQL 计算中使用别名)
问题描述
为什么这个查询不起作用?
Why won't this query work?
SELECT 10 AS my_num, my_num*5 AS another_number
FROM table
在本例中,我尝试在其他计算中使用 my_num 别名.这导致未知列my_num"
In this example, I'm trying to use the my_num alias in other calculations. This results in unknown column "my_num"
这是我正在尝试做的简化版本,但基本上我想使用别名进行其他计算.我的计算要复杂得多,这就是为什么最好将其别名化,因为我以不同的方式重复了几次.
This is a simplified version of what I am trying to do, but basically I would like to use an alias to make other calculations. My calculations are much more complicated and thats why it would be nice to alias it since I repeat it several times different ways.
推荐答案
简单地用 (SELECT alias) 包裹你重复使用的别名:
Simply wrap your reused alias with (SELECT alias) :
SELECT 10 AS my_num,
(SELECT my_num) * 5 AS another_number
FROM table
这篇关于在 SQL 计算中使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 SQL 计算中使用别名
基础教程推荐
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
