MySQL Table alias

In the SQL statement, you can specify an alias for table names and field (column) names, which are SQL standard syntax, and are supported by almost all database systems. This is specified by the keyword AS .

Table Alias ​​Syntax:

SELECT column FROM table AS table_alias

After the implementation of the above SQL effect, gives the impression is table_alias table query, but in fact single table simple alias query is usually meaningless. Usually a table to be treated as multiple tables to operate, or operate on multiple tables, set the table alias.


Article table:

uid username password email regdate
1 admin b7e591c246d010bb2ccd77d52490c85e 1277992339
2 Jack a193686a53e4de85ee3f2ff0576adf01 1278063917
3 Jack 0193686a35e4de85ee3f2ff0567adf490 1278061380

User table:

title content username
Article 1 Article 1 Body Content ... admin

When an article when the query will generally be the author of the article will be the corresponding query, the usual SQL statement is:

SELECT article.title, article.content, user.username FROM article, user 
WHERE article.aid = 1 AND article.uid = user.uid

After setting the table alias:

SELECT a.title, a.content, u.username FROM article AS a, user AS u where a.aid = 1 and a.uid = u.uid

The two SQL statements query results are the same:

uid username password email regdate
1 admin b7e591c246d010bb2ccd77d52490c85e 1277992339

As you can see, the use of table alias query, SQL can become simple and easier to write and read, especially in SQL more complex circumstances. In addition to using aliases to simplify SQL, there are cases when a table to do their own association, you must use the alias as the two tables associated with the operation.

In SQL, not only can specify the table alias, you can also specify the field (column) alias, to solve the problem of complex queries between multiple tables, please refer to the " MySQL field alias ."

Copyright 2014-2017 by All Rights Reserved.