2017-12-02 256 views
1

我认为我有正确的想法来解决这个功能,但我不知道为什么当我尝试测试它时出现此错误。任何人都可以帮我解决这个问题吗?在数据库中计数教师

cur.execute(Q) sqlite3.OperationalError:邻近 “具有”:语法错误

课程表: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) 
+0

你能分享你的表的定义吗? – Mureinik

+0

检查我的编辑。它具有按特定顺序列出的表格 – deezy

回答

0

你需要将你的结果由course柱:

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

什么是num_instructors? – deezy

+0

传达第二列含义的别名 – Mureinik

+0

它应该做什么? – deezy