在SQL中使用别名

时间:2013-04-24 00:03:20

标签: sql aliases

我使用以下代码,但我试图理解别名,我得到一个错误,这段代码“SQL命令未正确结束”,第2行

SELECT student.lastname, student.firstname
FROM student AS Student_Name
INNER JOIN memberof ON (Student_Name.SID = memberof.studentid)
INNER JOIN studentgroup ON (memberof.groupid = studentgroup.gid)
GROUP BY Student_Name.lastname , Student_Name.firstname 
HAVING COUNT(memberof.groupid) >= 2

2 个答案:

答案 0 :(得分:1)

我在想你正在使用Oracle,它显然不支持表别名的as关键字。

SELECT s.lastname, 
       s.firstname
FROM student s
INNER JOIN memberof 
    ON s.SID = memberof.studentid
INNER JOIN studentgroup 
    ON memberof.groupid = studentgroup.gid
GROUP BY s.lastname, 
         s.firstname 
HAVING COUNT(*) >= 2;

答案 1 :(得分:0)

<强> MYSQL:

如果使用别名,则需要始终使用别名引用该表。一般来说,我使用别名缩短我的名字,这样我就不必输入那么多

SELECT s.lastname, s.firstname
FROM student AS s 
INNER JOIN memberof ON (s.SID = memberof.studentid)
INNER JOIN studentgroup ON (memberof.groupid = studentgroup.gid)
GROUP BY s.lastname , s.firstname 
HAVING COUNT(memberof.groupid) >= 2

“s”可能太短,取决于你的代码的上下文,但你明白了

<强> ORACLE

Oracle不支持 AS 表别名,仅支持列别名: SQL Command not properly ended?