为列添加别名的不同方法

发布于 2024-11-09 15:42:03 字数 229 浏览 2 评论 0原文

之间有什么区别

select  empName as EmployeeName from employees

两者

select  EmployeeName = empName from employees

从技术角度来看, ?不确定这是否只是 SQL Server 特定的。

感谢您的回答。

What is the difference between

select  empName as EmployeeName from employees

versus

select  EmployeeName = empName from employees

from a technical point of view. Not sure if this is just SQL server specific or not.

Appreciate your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2024-11-16 15:42:03

我更喜欢第一个,因为第二个不可移植 -

select  EmployeeName = empName from employees

要么是语法错误(至少在 SQLite 和 Oracle 中),要么它可能不会给你你所期望的(比较两列 EmployeeName 和 empName 并返回比较结果)结果为布尔值/整数),而

select  empName EmployeeName from employees

与我首选的变体相同

  select  empName as EmployeeName from employees

I'd prefer the first one, since the second one is not portable -

select  EmployeeName = empName from employees

is either a syntax error (at least in SQLite and Oracle), or it might not give you what you expect (comparing two columns EmployeeName and empName and returning the comparison result as a boolean/integer), whereas

select  empName EmployeeName from employees

is the same as

  select  empName as EmployeeName from employees

which is my preferred variant.

无言温柔 2024-11-16 15:42:03

第二种语法的主要优点是它允许列别名全部排列起来,这对于长表达式来说是有益的。

SELECT foo,
       bar,
       baz = ROW_NUMBER() OVER (PARTITION BY foo ORDER BY bar)
FROM T

The main advantage of the second syntax is that it allows the column aliases to be all lined up which can be of benefit for long expressions.

SELECT foo,
       bar,
       baz = ROW_NUMBER() OVER (PARTITION BY foo ORDER BY bar)
FROM T
━╋う一瞬間旳綻放 2024-11-16 15:42:03

我不认为技术上有什么区别。主要是优惠。我选择第二个,因为它更容易在大型查询中发现列,特别是在查询正确缩进的情况下。

I don't think there's a technical difference. Its mainly preferential. I go for the second as its easier to spot columns in big queries, especially if the query is properly indented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文