1

我用标准 SQL 编写查询,并在 MySQL 中对其进行了测试。

现在,当我尝试在 PostgreSQL 中运行它们时,我得到了各种各样的错误,我不太明白。这是在 MySQL 中工作的原始查询:

CREATE VIEW popularCSsections AS (
    SELECT  sect.csid, COUNT(sc.sid) as numStudents
    FROM courseSection sect, studentCourse sc, department d
    WHERE sect.csid = sc.csid 
        AND sect.dcode = d.dcode 
        AND dname = "Computer Science"
    GROUP BY sect.csid
    HAVING numStudents > 2
);

给出这个错误:

psql:a2tables:60: ERROR:  column "Computer Science" does not exist
LINE 8:   AND department.dname = "Computer Science" 
                                 ^

您能帮我理解错误的性质并帮助我修复它们吗?


附加问题:

CREATE VIEW popularCSsections AS (
SELECT  sect.csid, COUNT(sc.sid) as numStudents
FROM courseSection sect, studentCourse sc, department d
WHERE sect.csid = sc.csid 
    AND sect.dcode = d.dcode 
    AND dname = 'Computer Science'
    GROUP BY sect.csid
    HAVING numStudents > 2
);

错误:

psql:a2tables:70: ERROR:  column "numstudents" does not exist
LINE 8:  HAVING numStudents > 2
                ^
4

1 回答 1

2

您需要用单引号引用字符串:

 AND dname = 'Computer Science'

这里发生的是双引号被解释为“标识符引号”,这表明您希望将内容解释为数据库标识符(列或表名)。

于 2012-11-06T04:22:36.417 回答