2017-12-02 50 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