如何在python/pymysql的标题下显示sql结果?

z2acfund  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(340)
def selectTopNactors(n):

# Create a new connection

con=connection()

# Create a cursor on the connection

cur=con.cursor()

# execute query

sql ="""SELECT g.genre_name AS 'genreName', a.actor_id AS 'actorId', COUNT(*) AS 'number_of_movies'
FROM actor AS a, role AS r, movie AS m, genre AS g, movie_has_genre AS mg
WHERE a.actor_id = r.actor_id AND m.movie_id = r.movie_id
    AND m.movie_id = mg.movie_id AND g.genre_id = mg.genre_id
    AND (g.genre_id , m.movie_id) IN (SELECT g.genre_id, m.movie_id
                                        FROM movie AS m, genre AS g, movie_has_genre AS mg
                                        WHERE m.movie_id = mg.movie_id AND mg.genre_id = g.genre_id
                                        ORDER BY g.genre_id)
GROUP BY a.actor_id,g.genre_name

ORDER BY g.genre_name,number_of_movies DESC LIMIT 5 """

cur.execute(sql,)
results = cur.fetchall()
for row in cur:
     #genre_name = row[0]
     #actor_id = row[1]
     #number_of_movies = row[2]
     print(row["g.genre_id"])
     print(row["a.actor_id"])
     print(row["number_of_movies"])

print (n)
con.commit()
return [("genreName ", "actorId ", "numberOfMovies"),results]

当我尝试这个的时候,我得到了这样的结果:

但我希望每一个结果都显示在每一列中,例如actorid列中的actorid和genrename列中的流派名称等等。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题