在查询中使用别名并使用它 [英] Using alias in query and using it

查看:108
本文介绍了在查询中使用别名并使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对sql中的别名有疑问和疑问。如果我想在同一查询中使用别名,可以使用它。例如:
考虑具有列a和b的表名xyz

I have a doubt and question regarding alias in sql. If i want to use the alias in same query can i use it. For eg: Consider Table name xyz with column a and b

select (a/b) as temp , temp/5 from xyz

这是否有可能?

推荐答案

您正在谈论为查询中的表达式赋予标识符,然后在查询的其他部分重用该标识符吗?

You are talking about giving an identifier to an expression in a query and then reusing that identifier in other parts of the query?

这在Microsoft SQL Server中是不可能的,而我几乎所有SQL经验都局限于此。但是,您可以执行以下操作。

That is not possible in Microsoft SQL Server which nearly all of my SQL experience is limited to. But you can however do the following.

SELECT temp, temp / 5
FROM (
    SELECT (a/b) AS temp
    FROM xyz
) AS T1

示例并不是特别有用,但是如果您在多个地方使用该表达式,则可能会更有用。当表达式较长且您也希望对其进行分组时,它会派上用场,因为GROUP BY子句要求您重新声明该表达式。

Obviously that example isn't particularly useful, but if you were using the expression in several places it may be more useful. It can come in handy when the expressions are long and you want to group on them too because the GROUP BY clause requires you to re-state the expression.

在MSSQL中,您还可以选择创建在表模式中而不是在查询中指定的计算列。

In MSSQL you also have the option of creating computed columns which are specified in the table schema and not in the query.

这篇关于在查询中使用别名并使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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