Sql 简明教程
SQL - Alias Syntax
您可以通过给它们另一个伪名称来临时重命名数据库中的表或列。此伪名称称为 Alias 。别名的用途是在 SQL 语句中寻址特定表或列,而无需更改其在数据库中的原始名称。别名通过 AS 关键字创建。
You can rename a table or a column in a database temporarily by giving them another pseudo name. This pseudo name is known as Alias. The use of aliases is to address a specific table or a column in an SQL statement without changing their original name in the database. Aliases are created with the AS keyword.
当处理涉及多个表或具有相似名称的列的复杂查询时,别名尤其有用。通过为这些表或列分配临时名称,您可以使您的 SQL 查询更具可读性和易于理解。
Aliases can be especially useful when working with complex queries involving multiple tables or columns with similar names. By assigning temporary names to these tables or columns, you can make your SQL query more readable and easier to understand.
The SQL Aliasing
别名用于在 SQL 查询中使用更短或更有意义的名称寻址数据库表。表别名的基本语法如下。
Aliases are used to address database tables with a shorter or more meaningful name within an SQL query. The basic syntax of a table alias is as follows.
SELECT column1, column2....
FROM table_name AS alias_name;
Example
假设我们已使用 CREATE TABLE 语句在 MySQL 数据库中创建了一个名为 CUSTOMERS 的表,如下所示 −
Assume we have created a table with name CUSTOMERS in MySQL database using CREATE TABLE statement as shown below −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
以下查询使用 INSERT 语句将值插入此表:
Following query inserts values into this table using the INSERT statement −
INSERT INTO CUSTOMERS VALUES
(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, 'Hyderabad', 4500.00),
(7, 'Muffy', 24, 'Indore', 10000.00);
获得的 CUSTOMERS 表如下:−
The CUSTOMERS table obtained is as follows −
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 |
Hyderabad |
4500.00 |
7 |
Muffy |
24 |
Indore |
10000.00 |
现在,我们使用如下所示的 CREATE TABLE 语句创建第二个表 ORDERS:−
Now, we are creating the second table ORDERS using CREATE TABLE statement as shown below −
CREATE TABLE ORDERS (
OID INT NOT NULL,
DATES DATETIME NOT NULL,
CUSTOMER_ID INT NOT NULL,
AMOUNT INT NOT NULL,
PRIMARY KEY (OID)
);
以下查询使用 INSERT 语句将值插入此表:
Following query inserts values into this table using the INSERT statement −
INSERT INTO ORDERS VALUES
(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);
获得的 ORDERS 表如下所示:−
The ORDERS table obtained is as shown below −
OID |
DATE |
CUSTOMER_ID |
AMOUNT |
102 |
2009-10-08 00:00:00 |
3 |
3000.00 |
100 |
2009-10-08 00:00:00 |
3 |
1500.00 |
101 |
2009-11-20 00:00:00 |
2 |
1560.00 |
103 |
2008-05-20 00:00:00 |
4 |
2060.00 |
现在,以下查询显示了 table alias 的用法。CUSTOMERS 表别名为“C”,ORDERS 表别名为“O”:−
Now, the following query shows the usage of a table alias. The CUSTOMERS table is aliased as 'C' and the ORDERS table is aliased as 'O' −
SELECT C.ID, C.NAME, C.AGE, O.AMOUNT
FROM CUSTOMERS AS C, ORDERS AS O
WHERE C.ID = O.CUSTOMER_ID;
Aliasing Column Names
我们还可以对 SQL 中的列名使用别名,以便在查询结果集中给出它一个不同的名称。 column 别名的基本语法如下:−
We can also use an alias for a column name in SQL to give it a different name in the result set of a query. The basic syntax of a column alias is as follows −
SELECT column_name AS alias_name
FROM table_name;
Aliasing with Self Join
SQL 自连接是用于将表连接到自身的方法,仿佛该表有两个表一样。在此过程中,我们需要使用别名为其中一个表指定一个临时名称,以避免理解错误。使用别名完成此重命名操作。
The SQL Self Join is used to join a table to itself as if the table were two tables. During this process, we need to use alias for one of the tables with a temporary name to avoid misunderstandings. This renaming is done using aliases.
Syntax
以下是使用别名执行自连接的语法:
Following is the syntax for performing a self-join with aliases −
SELECT column_name(s)
FROM my_table a, my_table b
ON a.join_column = b.join_column;
Example
现在,让我们使用以下自连接查询将 CUSTOMERS 表连接到自身。我们的目的是根据客户的收入建立客户之间的关系。在此,我们对列名和表名使用了别名:
Now, let us join the CUSTOMERS table to itself using the following Self Join query. Our aim is to establish a relationship among customers on the basis of their earnings. In here, we are using aliases with column names as well as with the table names −
SELECT
a.ID, b.NAME as EARNS_HIGHER,
a.NAME as EARNS_LESS,
a.SALARY as LOWER_SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
Output
以上查询的输出如下 −
Output of the above query is as follows −
ID |
EARNS_HIGHER |
EARNS_LESS |
LOWER_SALARY |
2 |
Ramesh |
Khilan |
1500.00 |
2 |
Kaushik |
Khilan |
1500.00 |
6 |
Chaitali |
Komal |
4500.00 |
3 |
Chaitali |
Kaushik |
2000.00 |
2 |
Chaitali |
Khilan |
1500.00 |
1 |
Chaitali |
Ramesh |
2000.00 |
6 |
Hardik |
Komal |
4500.00 |
4 |
Hardik |
Chaitali |
6500.00 |
3 |
Hardik |
Kaushik |
2000.00 |
2 |
Hardik |
Khilan |
1500.00 |
1 |
Hardik |
Ramesh |
2000.00 |
3 |
Komal |
Kaushik |
2000.00 |
2 |
Komal |
Khilan |
1500.00 |
1 |
Komal |
Ramesh |
2000.00 |
6 |
Muffy |
Komal |
4500.00 |
5 |
Muffy |
Hardik |
8500.00 |
4 |
Muffy |
Chaitali |
6500.00 |
3 |
Muffy |
Kaushik |
2000.00 |
2 |
Muffy |
Khilan |
1500.00 |
1 |
Muffy |
Ramesh |
2000.00 |