In mysql, you can use the "SELECT" statement and the "AS" keyword to query data and create aliases. The syntax is "SELECT field name/* FROM table name AS table alias;" or "SELECT field name AS field alias FROM data table name;".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Specify an alias for the table
When the table name is very long or some special queries are executed, for the convenience of operation, you can specify an alias for the table. Alias, use this alias to replace the original name of the table.
The basic syntax format for specifying an alias for a table is:
<表名> [AS] <别名>
The meaning of each clause is as follows:
mysql> SELECT stu.name,stu.height FROM tb_students_info AS stu; +--------+--------+ | name | height | +--------+--------+ | Dany | 160 | | Green | 158 | | Henry | 185 | | Jane | 162 | | Jim | 175 | | John | 172 | | Lily | 165 | | Susan | 170 | | Thomas | 178 | | Tom | 165 | +--------+--------+ 10 rows in set (0.04 sec)
Specifies the alias for the field
When using the SELECT statement to query data, MySQL displays the fields specified for output after each SELECT. Sometimes in order to display the results more intuitively, we can specify an alias for the field. The basic syntax format for specifying an alias for a field is:<字段名> [AS] <别名>Among them, the syntax meaning of each clause is as follows:
Query the tb_students_info table, specify the alias student_name for name, and specify the alias student_age for age
mysql> SELECT name AS student_name, age AS student_age FROM tb_students_info; +--------------+-------------+ | student_name | student_age | +--------------+-------------+ | Dany | 25 | | Green | 23 | | Henry | 23 | | Jane | 22 | | Jim | 24 | | John | 21 | | Lily | 22 | | Susan | 23 | | Thomas | 22 | | Tom | 23 | +--------------+-------------+ 10 rows in set (0.00 sec)
Note: The table alias is only used when executing the query is used and is not displayed in the returned results. After defining an alias for a field, it will be returned to the client for display. The displayed field is the alias of the field.
[Related recommendations:
mysql video tutorialThe above is the detailed content of How to query data and create aliases in mysql. For more information, please follow other related articles on the PHP Chinese website!