上一节:
下一节:

  SQL - 别名语法

  • 简述

    您可以通过提供另一个名称来临时重命名表或列Mooas. 表别名的使用是在特定的 SQL 语句中重命名表。重命名是临时更改,数据库中的实际表名不会更改。列别名用于为特定 SQL 查询重命名表的列。
  • 句法

    a 的基本语法table别名如下。
    
    SELECT column1, column2....
    FROM table_name AS alias_name
    WHERE [condition];
    
    a 的基本语法column别名如下。
    
    SELECT column_name AS alias_name
    FROM table_name
    WHERE [condition];
    
  • 例子

    考虑以下两个表。
    Table 1− CUSTOMERS 表如下。
    
    +----+----------+-----+-----------+----------+
    | ID | NAME     | AGE | ADDRESS   | SALARY   |
    +----+----------+-----+-----------+----------+
    |  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
    |  2 | Khilan   |  25 | Delhi     |  1500.00 |
    |  3 | kaushik  |  23 | Kota      |  2000.00 |
    |  4 | Chaitali |  25 | Mumbai    |  6500.00 |
    |  5 | Hardik   |  27 | Bhopal    |  8500.00 |
    |  6 | Komal    |  22 | MP        |  4500.00 |
    |  7 | Muffy    |  24 | Indore    | 10000.00 |
    +----+----------+-----+-----------+----------+
    
    Table 2− ORDERS 表如下。
    
    +-----+---------------------+-------------+--------+
    |OID  | DATE                | CUSTOMER_ID | AMOUNT |
    +-----+---------------------+-------------+--------+
    | 102 | 2009-10-08 00:00:00 |           3 |   3000 |
    | 100 | 2009-10-08 00:00:00 |           3 |   1500 |
    | 101 | 2009-11-20 00:00:00 |           2 |   1560 |
    | 103 | 2008-05-20 00:00:00 |           4 |   2060 |
    +-----+---------------------+-------------+--------+
    
    现在,以下代码块显示了table alias.
    
    SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT 
       FROM CUSTOMERS AS C, ORDERS AS O
       WHERE  C.ID = O.CUSTOMER_ID;
    
    这将产生以下结果。
    
    +----+----------+-----+--------+
    | ID | NAME     | AGE | AMOUNT |
    +----+----------+-----+--------+
    |  3 | kaushik  |  23 |   3000 |
    |  3 | kaushik  |  23 |   1500 |
    |  2 | Khilan   |  25 |   1560 |
    |  4 | Chaitali |  25 |   2060 |
    +----+----------+-----+--------+
    
    以下是a的用法column alias.
    
    SQL> SELECT  ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
       FROM CUSTOMERS
       WHERE SALARY IS NOT NULL;
    
    这将产生以下结果。
    
    +-------------+---------------+
    | CUSTOMER_ID | CUSTOMER_NAME |
    +-------------+---------------+
    |           1 | Ramesh        |
    |           2 | Khilan        |
    |           3 | kaushik       |
    |           4 | Chaitali      |
    |           5 | Hardik        |
    |           6 | Komal         |
    |           7 | Muffy         |
    +-------------+---------------+
    
上一节:
下一节: