仅选择具有多个教师的行

时间:2017-12-02 16:20:14

标签: python-3.x sqlite

我无法生成仅显示包含多个教师的课程的输出...

    [('HLTC16H3F', 'C. Furness'),      ('HLTC16H3F', 'E. Seto'),      \
 ('MATA29H3F', 'G. Scott'),       ('MATA29H3F', 'X. Jiang'),      \
 ('MATA32H3F', 'E. Moore'),       ('MATA32H3F', 'R. Grinnell'),   \
 ('MATA32H3F', 'R. Buchweitz'),   ('MATA67H3F', 'R. Pancer'),     \
 ('MATA67H3F', 'A. Bretscher'),   ('MGAB01H3F', 'L. Chen')]

我想要产生的所需输出是......

def create_course_table(db, course_file):
'''Courses Table should be ID,Course,Section,Name'''

con = sqlite3.connect(db)
cur = con. cursor()

cur.execute('''DROP TABLE IF EXISTS Courses''')

# create the table
cur.execute('''CREATE TABLE Courses( ID TEXT , Course TEXT , 
Sections TEXT , Name TEXT)''')

# Read CSV File
csv_reader = open(course_file, 'r')
csv_reader.readline()      

# Insert the rows

for line in csv_reader:
    course = line.strip().split(',')
    ID = course[0]
    Course = course[1]
    Section = course[2]
    Name = course[3:]
    for names in Name:
        cur.execute('''INSERT INTO Courses VALUES (?, ?, ?, ?)''', 
                    (ID, Course, Section, names))    

# commit and close the cursor and connection
con.commit()
cur.close()
con.close()

我用来获取这些列的表来自这个函数......

 return run_query(db, ''' SELECT Course, Name FROM  Courses GROUP BY Course
HAVING COUNT(Name) > 1  ''')
    [('ANTA01H3F', 'S. Dorland'), ('BIOA01H3F', 'S. Brunt'), ('CHMA10H3F', 'N. Thavarajah'), ('CHMB16H3F', 'K. Kerman'), ('CSCA08H3F', 'B. Harrington'), ('CSCA67H3F', 'A. Bretscher'), ('HLTC16H3F', 'E. Seto'), ('MATA29H3F', 'X. Jiang'), ('MATA32H3F', 'R. Buchweitz'), ('MATA67H3F', 'A. Bretscher'), ('MGAB01H3F', 'L. Harvey'), ('MGAB03H3F', 'G. Quan Fun'), ('MGAD65H3F', 'S. Ratnam'), ('MGEB02H3F', 'A. Mazaheri'), ('MGFC10H3F', 'M. Hasler'), ('MGMA01H3F', 'T. Dewan'), ('MGSC14H3F', 'P. Constantinou'), ('POLA01H3F', 'P. Triadafilopoulos'), ('POLB80H3F', 'C. LaRoche'), ('STAB22H3F', 'N. Asidianya'), ('VPMA93H3F', 'R. King')]

任何帮助将不胜感激。我试过这样做但没有成功......

Stream.parallel().forEach()

任何帮助/提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

以下查询应返回所需的行。

SELECT Course, Name FROM Courses WHERE Course IN (
    SELECT Course FROM Courses GROUP BY Course HAVING COUNT(Name) > 1
);