在查詢時,可以為表和字段取一個別名。這個別名可以代替其指定的表和字段
為表取別名
復制代碼 代碼如下:
mysql> SELECT * FROM department d
-> WHERE d.d_id=1001;
+------+-----------+--------------+-------------+
| d_id | d_name | function | address |
+------+-----------+--------------+-------------+
| 1001 | 科技部 | 研發產品 | 3號樓5層 |
+------+-----------+--------------+-------------+
row in set (0.00 sec)
這裡為department表取了一個別名為d
為字段取別名
復制代碼 代碼如下:
mysql> SELECT d_id AS department_id, d_name AS department_name
-> FROM department;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
| 1001 | 科技部 |
| 1002 | 生產部 |
| 1003 | 銷售部 |
+---------------+-----------------+
rows in set (0.00 sec)
此處語法與上面類似,只不過加了AS