发布于 2015-07-25 11:37:00 | 217 次阅读 | 评论: 0 | 来源: 网络整理
可以重命名表或列暂时给人的另一个名字,这被称为别名。使用表别名是指在一个特定的SQLite语句重命名表。重命名是一个临时的变化,并在数据库中实际的表的名称不会改变。
列别名用来重命名表中的列一个特定的SQLite查询的目的。
表别名的基本语法如下:
SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];
列别名的基本语法如下:
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
考虑以下两个表:(一)COMPANY表如下:
sqlite> select * from COMPANY;
ID NAME AGE ADDRESS SALARY
---------- -------------------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
(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
现在,下面是表的别名,在这里我们使用C和D分别为COMPANY和DEPARTMENT表的别名的用法:
sqlite> SELECT C.ID, C.NAME, C.AGE, D.DEPT
FROM COMPANY AS C, DEPARTMENT AS D
WHERE C.ID = D.EMP_ID;
以上SQLite的语句将产生以下结果:
ID NAME AGE DEPT
---------- ---------- ---------- ----------
1 Paul 32 IT Billing
2 Allen 25 Engineerin
3 Teddy 23 Engineerin
4 Mark 25 Finance
5 David 27 Engineerin
6 Kim 22 Finance
7 James 24 Finance
让我们看一个例子company_id的是ID列的别名和COMPANY_NAME的别名name列字段的别名的用法:
sqlite> 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;
以上SQLite的语句将产生以下结果:
COMPANY_ID COMPANY_NAME AGE DEPT
---------- ------------ ---------- ----------
1 Paul 32 IT Billing
2 Allen 25 Engineerin
3 Teddy 23 Engineerin
4 Mark 25 Finance
5 David 27 Engineerin
6 Kim 22 Finance
7 James 24 Finance