PostgreSQL ALIAS 语法

PostgreSQL ALIAS 语法

您可以通过给出另一个名字来临时重命名表或列,这被称为 ALIAS 。表别名的使用意味着在特定的PostgreSQL语句中重新命名一个表。重命名是一种临时更改,实际的表名在数据库中不会改变。

列别名用于在特定的PostgreSQL查询中重命名表的列。

语法

表别名的基本语法如下所示:

SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];

列别名的基本语法如下所示:

SELECT column_name AS alias_name
FROM table_name
WHERE [condition];

示例

考虑以下两个表,(a) COMPANY 表如下所示−

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

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表的别名,分别是 –

testdb=# SELECT C.ID, C.NAME, C.AGE, D.DEPT
   FROM COMPANY AS C, DEPARTMENT AS D
   WHERE  C.ID = D.EMP_ID;

上面给定的PostgreSQL语句将产生以下结果 –

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列的别名-

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语句将产生以下结果−

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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程