Use of the alias for the MySQL note

  • 2020-05-17 06:40:56
  • OfStack

When querying, you can take 1 individual name for the table and field. This alias can replace the tables and fields specified by it


Aliases the table


mysql> SELECT * FROM department d
    -> WHERE d.d_id=1001;
+------+-----------+--------------+-------------+
| d_id | d_name    | function     | address     |
+------+-----------+--------------+-------------+
| 1001 |  Ministry of Science and Technology     |  Research and development products        | 3 building 5 layer      |
+------+-----------+--------------+-------------+
 row in set (0.00 sec)

Here we have a 1 for the department table with the alias d


Aliases the field


mysql> SELECT d_id AS department_id, d_name AS department_name
    -> FROM department;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
|          1001 |  Ministry of Science and Technology           |
|          1002 |  The production department           |
|          1003 |  The sales department           |
+---------------+-----------------+
 rows in set (0.00 sec)

The syntax is similar here, except with AS


Related articles: