简体   繁体   English

mysql:'r'是什么意思?

[英]mysql: what does 'r' mean?

I've been going through some code and repeatedly see the letter 'r' in queries, and I've never seen it before. 我一直在查看一些代码并在查询中反复看到字母'r',我以前从未见过它。 I'm no mysql guru and cannot find references on the web that really make sense in this case. 我不是mysql大师,在这种情况下无法在网上找到真正有意义的参考资料。

Sample query: 示例查询:

SELECT * FROM database.table r
WHERE column = 'whatever'
AND otherColumn = 'whenever'
ORDER BY id, name

That actually means that the table is being aliased from its long form to the letter/symbol 'r'. 这实际上意味着表格从其长格式别名到字母/符号'r'。

It's a red herring in your case because 'r' is not used anywhere in the query and it doesn't need to be. 在你的情况下它是一个红色的鲱鱼 ,因为在查询的任何地方都没有使用'r'而且它不需要。 Your query is not a good example of using aliases because there is only one table involved. 您的查询不是使用别名的好例子,因为只涉及一个表。 If you join multiple tables then aliasing becomes handy (although not required) to specify which table's column you're referencing in your various query's clauses. 如果您连接多个表,则别名变得很方便(尽管不是必需的),以指定您在各种查询子句中引用的表的列。

You can simply remove the 'r' and run your query. 您只需删除“r”并运行查询即可。

SELECT * FROM database.table r
WHERE column = 'whatever'
AND otherColumn = 'whenever'
ORDER BY id, name

Or use it outright like: (though it's redundant here) 或者直接使用它:(尽管这里多余)

SELECT * FROM database.table r
WHERE r.column = 'whatever'
AND r.otherColumn = 'whenever'
ORDER BY r.id, r.name

BTW, SQL code like this is the reason I tend to use the keyword AS to highlight the fact that I am aliasing. BTW,像这样的SQL代码是我倾向于使用关键字AS突出我是别名的事实的原因。 So the FROM clause would look like this: FROM database.table AS r 所以FROM子句看起来像这样: FROM database.table AS r

As far as what that unused alias is doing there is a good question. 至于未使用的别名正在做什么,这是一个很好的问题。 My guess it that it's been getting cut, copied and pasted from some old query which used the alias but no one ever bother to remove it when it became unnecessary. 我猜它是从一些使用别名的旧查询中剪切,复制和粘贴的,但是当它变得不必要时,没有人费心去除它。

r is an alias you can give to a table. r是您可以为表提供的别名。 You can reference table columns later by that alias, for example r.column1 您可以稍后通过该别名引用表列,例如r.column1

It's a table alias. 这是一个表别名。 If you're joining two tables with duplicated column names, you would disambiguate the query by saying some_table.id, the table alias allows you to just type r.id. 如果要连接两个具有重复列名的表,则可以通过说some_table.id消除歧义歧义,表别名允许您只键入r.id.

It's an alias. 这是别名。 It means: We must use r now instead of database.table . 这意味着:我们必须使用r now而不是database.table

We can use r.column but we are still allowed to use just column if there is no ambiguity. 我们可以使用r.column但如果没有歧义,我们仍然可以使用just column

SELECT * FROM database.table r
WHERE r.column = 'whatever'         --using the alias r
AND otherColumn = 'whenever'        --not using it
ORDER BY id, r.name                 --mixing

Looks like a table alias to me with the AS keyword omitted. 看起来像是一个表别名,省略了AS关键字。 In the sample query, the alias is unnecessary since only one table is being referenced and the alias is never used. 在示例查询中,别名是不必要的,因为只引用了一个表,并且从不使用别名。

The relevant syntax for a table_reference is specified in the MySQL documentation as tbl_name [[AS] alias] [index_hint_list] . tbl_name [[AS] alias] [index_hint_list]的相关语法在MySQL文档中指定为tbl_name [[AS] alias] [index_hint_list]

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

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