Home  >  Article  >  Database  >  How to query data and create aliases in mysql

How to query data and create aliases in mysql

青灯夜游
青灯夜游Original
2022-01-04 14:21:2312395browse

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;".

How to query data and create aliases in mysql

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:

  • ##722e3d59fd24604761db25f00f9b264f: The name of the data table stored in the database.

  • 4b6231bbc83d58bb859385d4f252f10e: The new name of the table specified when querying.

  • The AS keyword can be omitted. If omitted, the table name and alias must be separated by spaces.

Note: The alias of a table cannot have the same name as other tables in the database. A field's alias cannot have the same name as another field in the table. Field aliases cannot be used in conditional expressions, otherwise an error message such as "ERROR 1054 (42S22): Unknown column" will appear.

Example 1

The following specifies the alias stu for the tb_students_info table

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:

    ##72f673a2a4861896dc18c9089d770c67: The new name of the field.
  • The AS keyword can be omitted. If omitted, the field name and alias must be separated by spaces.
  • Example 2

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 tutorial

]

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn