问题描述
我刚刚遇到了一个使用 AS 给表别名的 SQL 语句,如下所示:
I just came across a SQL statement that uses AS to alias tables, like this:
SELECT all, my, stuff
FROM someTableName AS a
INNER JOIN someOtherTableName AS b
ON a.id = b.id
我习惯看到的是:
SELECT all, my, stuff
FROM someTableName a
INNER JOIN someOtherTableName b
ON a.id = b.id
我假设没有区别,只是语法糖,但其中哪一个更普遍/广泛?有什么理由更喜欢一个吗?
I'm assuming there's no difference and it's just syntactic sugar, but which of these is more prevalent/wide-spread? Is there any reason to prefer one over the other?
编辑以澄清:
我感谢所有的答案和提出的所有观点,但问题是不是为什么或为什么不使用表别名.问题纯粹是关于将AS"关键字用于表别名或将其省略.
I appreciate all the answers and all the points made, but the question was not why or why not to use table aliases. The question was purely about using the "AS" keyword for table aliasing or leaving it out.
推荐答案
这是语法糖,打字需要更长的时间,但有些人发现它更易读和清晰.我使用它的原因是,在阅读大型查询时,通过查找 AS
更容易挑选出别名.
It's syntactic sugar and it takes a little bit longer to type but some people find it more readable and clear. The reason I use it is that when reading a large query, it is easier to pick out the aliases by looking for the AS
's.
另一个原因,有时完整的表名很长而且输入起来很麻烦.当您没有自动完成等花哨的功能时,或者当您只是感到懒惰时,使用较短的别名有时会使事情变得更容易.;)
Another reason, sometimes the full table name is long and cumbersome to type. Aliasing to something shorter can sometimes make things easier for when you don't have fancy features like autocomplete - or for when you're just feeling lazy. ;)
...正如其他一些人在我之前指出的那样,它在进行自联接时很有用.
...And as some others have pointed out before me, it can be useful when doing self-joins.
这篇关于为什么要使用“AS"?为 SQL 表设置别名时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!