在SQL计算中使用别名 [英] Using an Alias in SQL Calculations

查看:237
本文介绍了在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计算中使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆