我们可以在SQL中使用变量吗?

时间:2019-10-07 10:49:38

标签: sql

如果可以在sql中创建变量并为其分配值,我感到困惑。 这是我已经尝试过的代码。

    select facultyName, count(programcode) as 'Highest Value'
    from program, faculty
    where program.FacultyCode = faculty.FacultyCode 
    group by facultyname
    having count(programcode) > 5;

这是我的讲师代码

   SELECT t.FacultyName, t.ProgramCount 
   FROM (
            SELECT Faculty.FacultyName, COUNT(*) AS 'Program Count'
            FROM Program, Faculty
            WHERE Program.FacultyCode=Faculty.FacultyCode
            GROUP BY Faculty.FacultyCode) t GROUP BY t.FacultyName
            HAVING MAX (t.ProgramCount) = (SELECT (MAX(p.ProgramCount
                                   FROM (
                                         SELECT Faculty.FacultyName, COUNT(*) AS 'Program Count'
                                         FROM Faculty, Program
                                         WHERE Faculty.FacultyCode = Program.FacultyCode
                                         GROUP BY Faculty.FacultyName) )))p;

输出应为5。

0 个答案:

没有答案