Gilbert
Gilbert

Reputation: 5

Why does such statement work in MySQL?

I'm using MySQL 5.6.23-enterprise-commercial-advanced. I have a table called FILE as:

+---------+
| file_id |
+---------+
|      11 |
|      12 |
|      13 |
|      14 |
+---------+

I try to run a query of SELECT A.FILE_ID FROM FILE A; under root@localhost, and it is successfully executed. But there is no such table as A(any will work).So why it can be executed and seemingly have the same effect as SELECT FILE.ID FROM FILE;? I'm working on an existing project, and I have seen this. Is there any benefit of using it?

Upvotes: 0

Views: 60

Answers (4)

user2439481
user2439481

Reputation:

In your case A as alias of table. So you must need to write as between FILE and A. So now your query is like this

SELECT A.FILE_ID FROM FILE as A;

Upvotes: 1

Aman Aggarwal
Aman Aggarwal

Reputation: 18449

Your table name is FILE and A is the alias name of your table FILE.

You can read using Alias in select statement:

MySQL Alias

Aliases are used to temporarily rename a table or column name for better understanding.

Upvotes: 1

Jens
Jens

Reputation: 69440

A is only an alias for table file in your case. That is why your query works.

Read here for more information about aliases in SQL:

SQL aliases are used to temporarily rename a table or a column heading.

Upvotes: 1

aphextwix
aphextwix

Reputation: 1858

Because you're giving the table an alias of A so you're essentially temporarily renaming the table during the select statement.

See link for more information - MYSQL ALIAS

Upvotes: 3

Related Questions