我对SQL不熟悉,并且此查询出现语法错误。我怀疑我可能错误地使用了'AS'关键字。谁能检查我的语法?我也尝试用[]代替单引号。 谢谢!
SELECT m.dname, COUNT(*) AS 'Total Students', AVG(s.age) AS 'Average Age',
AVG(s.gpa) AS 'Average GPA'
FROM student s JOIN major m
ON s.sid = m.sid
GROUP BY m.dname
HAVING COUNT(*) >= 3
编辑****
我得到的错误:
ERROR: syntax error at or near "'Total Students'" LINE 1: SELECT m.dname, COUNT(*) AS 'Total Students', AVG(s.age) AS... ^
使用PostgreSQL数据库
答案 0 :(得分:2)
在postgres中,对于包含空格的列别名,请使用双引号,而不是单引号:
SELECT m.dname, COUNT(*) AS "Total Students", AVG(s.age) AS "Average Age",
AVG(s.gpa) AS "Average GPA"
FROM student s JOIN major m
ON s.sid = m.sid
GROUP BY m.dname
HAVING COUNT(*) >= 3
- http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-SELECT-LIST
答案 1 :(得分:1)
使用AS
时无需引号试试这个
SELECT m.dname, COUNT(*) AS Total_Students, AVG(s.age) AS Average_Age,
AVG(s.gpa) AS Average_GPA
FROM student s JOIN major m
ON s.sid = m.sid
GROUP BY m.dname
HAVING COUNT(*) >= 3