TQuery.SQL中简单的SQL语句中一问。(30分)

  • 主题发起人 主题发起人 springwater
  • 开始时间 开始时间
S

springwater

Unregistered / Unconfirmed
GUEST, unregistred user!
SELECT Customer.CustNo, Customer.Company, Customer.City,
Customer.Country, Orders.OrderNo, Orders.AmountPaid, Employee.LastName,
Employee.FirstName, Employee.Salary, Employee.HireDate,
Employee.PhoneExt, Employee.EmpNo
FROM customer Customer
INNER JOIN [red]orders Orders[/red]
ON (Customer.CustNo = Orders.CustNo)
INNER JOIN employee Employee
ON (Orders.EmpNo = Employee.EmpNo)

李维先生的《Delphi5.X分布式多层应用系统篇》的多表更新一节中,一个
TQuery.SQL中写入了以上SQL语句,作用是连接了三张数据表(DBDEMOS数据
库中)。其中语法挺怪,"FROM","INNER JOIN"后跟的表名为什么要重复两遍
呢?这样写是什么意思呢?
 
一个是表名,一个是别名,你注意到它前面select customen.custno,
就是指定了别名的结果,如果指定的别名是a,那么就可以写成select a.custno.
 
Agree with above 2 people:

别名常用于SQL Query,使用别名一般有两个好处:1.减少typing, (especially when
you want to specify Server name, database owner, etc); 2.便于读Code.但这里
别名显得太长,且别名与表名太Confusing, 所以你觉得语法挺怪,建议用较简单的别名.

An example as follows:

SELECT a.id1, a.name1
FROM table1 a INNER JOIN
table2 b ON a.id1 = b.id1
 
对,是别名
 
to ourself :如果用a,b,c,d等别名,表多的时候容易搞混了
 
其实我倒喜欢用简单的别名,要不然不就失去了别名的意义了吗?
如:Sales S
Titles T
......
 
我初学,对别名不是很懂,请问各位,在上面的SQL语句中,并没有显示的定义:
比如:Customer表的别名为customer,是默认的吗?
如果我要把Customer表的别名指定为a,我又该怎么写,写在哪儿呢?
谢谢您!
 
1. To springwater:

Have a look at the following example. a is the alias (别名) of table1.

SELECT a.id1, a.name1
FROM table1 AS a INNER JOIN
table2 AS b ON a.id1 = b.id1

2. To Yong:

Agree with your. It's not a good idea to use a, b as 别名 havng too many tables.
I think we can use 别名 such as:
cpn (for company table),
psn (for person table),
cust (for customer table),etc.
 
后退
顶部