计算数据库中的教师

时间:2017-12-02 20:19:58

标签: python sql database select sqlite

我认为我有正确的想法来解决这个问题,但我不确定为什么在我尝试测试它时会出现此错误。有人可以帮我解决这个问题吗?

cur.execute(q)中 sqlite3.OperationalError:near" HAVING&#34 ;:语法错误

课程表:ID,课程,部分,名称 地点表:ID,房间

期望的输出:

>>> courses_how_many_instructors(db)
[('HLTC16H3F', 2), ('MATA29H3F', 2), ('MATA32H3F', 3), ('MATA67H3F', 2), \
('MGAB01H3F', 3), ('MGAB03H3F', 2), ('MGAD65H3F', 2), ('BIOA01H3F', 3), \
('POLB80H3F', 2), ('STAB22H3F', 2), ('VPMA93H3F', 2), ('CHMA10H3F', 2), \
('CHMB16H3F', 2), ('CSCA08H3F', 2), ('CSCA67H3F', 2)]

def courses_how_many_instructors(db):
'''Return the course number and the number of instructors for courses with 
 more than one instructor. Note that this means the ID must be
the same for each instructor.'''
query = '''SELECT Course, Name FROM Courses WHERE NAME>1 GROUP BY HAVING COUNT(Name)'''
return run_query(db, query)

1 个答案:

答案 0 :(得分:0)

您需要按course列对结果进行分组:

SELECT   course, COUNT(DISTINCT name) AS num_instructors
FROM     courses
GROUP BY name
HAVING   COUNT(DISTINCT name) > 1