简体   繁体   English

有关MySQL表别名的查询

[英]A query about MySQL table aliases

So there's a question about MySQL aliases for table names, and it has raised me to ask this question here: 因此,关于表名的MySQL别名存在一个问题 ,这引起了我在这里提出这个问题的疑问:

Why is the use of aliases in naming tables in MySQL queries treated as a pseudo- standard behaviour rather than as behaviour needed only in certain situations? 为什么在MySQL查询的命名表中使用别名被视为伪标准行为,而不是仅在某些情况下才需要的行为?

Take the following example from the question linked above: 从上面链接的问题中获取以下示例:

SELECT st.StudentName, cl.ClassName
FROM StudentClass sc
INNER JOIN Classes cl ON cl.ClassID = sc.ClassID
INNER JOIN Students st ON st.StudentID = sc.StudentID;

From my experience the alias for the tables is usually unneeded (some might say pseudo-random, spacing filling) letters and can just as easily, and more readably be: 根据我的经验,表的别名通常是不需要的(有些人可能会说是伪随机,空格填充)字母,并且可以很容易且更容易理解为:

SELECT Students.StudentName, Classes.ClassName
FROM StudentClass 
INNER JOIN Classes ON Classes.ClassID = StudentClass.ClassID
INNER JOIN Students ON Students.StudentID = StudentClass.StudentID;

Obviously it may -in some situations- be better to use shortened naming convention, perhaps for a large query with many tables each of long names, but that's no reason (as far as I can see) for the absolute over the top prevelance of this methodology of forming an alias for each table, regardless of need. 显然,在某些情况下,使用缩短的命名约定可能会更好,对于一个包含多个长名的表的大型查询,也许就更好了,但是就我所知,绝对没有理由(据我所知)无论需要如何,为每个表形成别名的方法。

I have googled this, but the majority of useful results state that it makes "the SQL more readable". 我已经用谷歌搜索过,但是大多数有用的结果都表明它使“ SQL更具可读性”。 That's as maybe for many or long-named tables in a Query as I've already state, but as an apparant standard?? 正如我已经陈述过的那样,这可能与查询中的许多或长名称的表一样,但是作为一种明显的标准?

Also, without the alias, it's clear to see the source table of each of the columns in the exampled SQL above. 另外,没有别名,很明显可以看到上面示例SQL中每列的源表。

I just want to see if there's some key methodology I'm completely missing here? 我只想看看是否有一些我完全不了解的关键方法?


To qualify why I feel the need to ask if I'm missing something: 要弄清为什么我觉得有必要问我是否缺少什么:

I see it an aweful lot on StackOverflow (as referenced here), which in itself means jack nothing, but then I see that there are no responses from knowledgable (high scoring) answerers that aliases are not needed (such as in the referenced post above), whereas other topics on SO those who deem themselves to know better (high scorers) are all over teling people how things should be done. 我在StackOverflow上看到了很多很棒的东西(如此处所引用),这本身就意味着什么也没做,但是随后我看到,对于那些不需要别名的知识渊博的(高分)回答者没有任何响应(例如,在以上引用的帖子中) ),而那些自以为懂得更多(高分者)的SO话题则在电话上告知人们应该如何做。

It leaves me unsure If I'm missing something, hence this question. 这让我不确定是否要丢失某些东西,因此是这个问题。


A comment by A MySQL authorized instructor and DBA circa 2010. MySQL授权讲师和DBA于2010年左右发表的评论

I think you may be mistaken that giving aliases to tables is somehow the standard. 我认为您可能会误认为为表提供别名是某种标准。

There are two situations where it's required. 在两种情况下是必需的。

  1. When the same table is joined more than once in a query. 在查询中多次联接同一张表时。 Aliases are needed to distinguish the two usages of the table. 需要使用别名来区分表的两种用法。

  2. A derived table (a subquery) needs an alias. 派生表(子查询)需要别名。

Other than that you can omit aliases to your heart's content. 除此之外,您可以省略心脏内容的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM