Postgresql 简明教程
PostgreSQL - ALIAS Syntax
您可以通过提供另一个名称暂时重命名表或列,该名称被称为 ALIAS 。使用表别名意在在特定 PostgreSQL 语句中重命名一个表。重命名是一种临时更改,实际的表名在数据库中不会更改。
You can rename a table or a column temporarily by giving another name, which is known as ALIAS. The use of table aliases means to rename a table in a particular PostgreSQL statement. Renaming is a temporary change and the actual table name does not change in the database.
列别名用于重命名表的列以便用于特定 PostgreSQL 查询。
The column aliases are used to rename a table’s columns for the purpose of a particular PostgreSQL query.
Syntax
table 别名的基本语法如下:
The basic syntax of table alias is as follows −
SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];
column 别名的基本语法如下:
The basic syntax of column alias is as follows −
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
Example
请查看以下两个表,(a) COMPANY 表如下所示:
Consider the following two tables, (a) COMPANY table is as follows −
testdb=# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
(b)另一张表 DEPARTMENT 如下所示:
(b) Another table is DEPARTMENT as follows −
id | dept | emp_id
----+--------------+--------
1 | IT Billing | 1
2 | Engineering | 2
3 | Finance | 7
4 | Engineering | 3
5 | Finance | 4
6 | Engineering | 5
7 | Finance | 6
(7 rows)
现在,以下是 TABLE ALIAS 的用法,其中我们使用 C 和 D 作为 COMPANY 和 DEPARTMENT 表的别名,分别:
Now, following is the usage of TABLE ALIAS where we use C and D as aliases for COMPANY and DEPARTMENT tables, respectively −
testdb=# SELECT C.ID, C.NAME, C.AGE, D.DEPT
FROM COMPANY AS C, DEPARTMENT AS D
WHERE C.ID = D.EMP_ID;
以上给出的 PostgreSQL 语句将产生以下结果 -
The above given PostgreSQL statement will produce the following result −
id | name | age | dept
----+-------+-----+------------
1 | Paul | 32 | IT Billing
2 | Allen | 25 | Engineering
7 | James | 24 | Finance
3 | Teddy | 23 | Engineering
4 | Mark | 25 | Finance
5 | David | 27 | Engineering
6 | Kim | 22 | Finance
(7 rows)
让我们看一看 COLUMN ALIAS 用法的示例,其中 COMPANY_ID 是 ID 列的别名而 COMPANY_NAME 是 name 列的别名:
Let us see an example for the usage of COLUMN ALIAS where COMPANY_ID is an alias of ID column and COMPANY_NAME is an alias of name column −
testdb=# SELECT C.ID AS COMPANY_ID, C.NAME AS COMPANY_NAME, C.AGE, D.DEPT
FROM COMPANY AS C, DEPARTMENT AS D
WHERE C.ID = D.EMP_ID;
以上给出的 PostgreSQL 语句将产生以下结果 -
The above given PostgreSQL statement will produce the following result −
company_id | company_name | age | dept
------------+--------------+-----+------------
1 | Paul | 32 | IT Billing
2 | Allen | 25 | Engineering
7 | James | 24 | Finance
3 | Teddy | 23 | Engineering
4 | Mark | 25 | Finance
5 | David | 27 | Engineering
6 | Kim | 22 | Finance
(7 rows)