Mysql: what does r mean?

I look through the code and repeatedly see the letter "r" in the queries, and I have never seen it before. I am not a mysql guru and cannot find links on the Internet that really make sense in this case.

Request example:

  SELECT * FROM database.table r
 WHERE column = 'whatever'
 AND otherColumn = 'whenever'
 ORDER BY id, name
+4
source share
5 answers

This actually means that the table is smoothed from its long form to the letter / symbol "r".

In your case there will be a red herring , because 'r' is not used anywhere in the request, and this is not necessary. Your query is not a good example of using aliases because there is only one table. If you join multiple tables, then anti-aliasing becomes convenient (although not required) to indicate which column of the column you are referring to in your various sentences.

You can simply remove the "r" and run your query.

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

Or use it just like: (although it is redundant here)

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

By the way, SQL code like this is the reason I tend to use the AS keyword to emphasize the fact that I'm an alias. Therefore, the FROM clause would look like this: FROM database.table AS r

As for what this unused alias does, there is a good question. I suppose this was abbreviated, copied and pasted from some old query that used an alias, but no one ever tried to remove it when it became unnecessary.

+8
source

r is the alias you can provide to the table. You can reference column columns later with this alias, for example r.column1

+4
source

This is a table alias. If you are joining two tables with duplicate column names, you should resolve the issue by specifying some_table.id, the table alias allows you to simply type r.id.

+4
source

It is a nickname. This means: we must use r instead of database.table .

We can use r.column , but we are still allowed to use only column if there is no ambiguity.

 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 
+4
source

Looks like a table alias for me with the AS keyword. In the query example, an alias is not needed because it refers to only one table, and the alias is never used.

The corresponding syntax for table_reference is specified in the MySQL documentation as tbl_name [[AS] alias] [index_hint_list] .

+1
source

Source: https://habr.com/ru/post/1346633/


All Articles