Avatar of the person chatting

记住两张表,1. 表名:language、字段名:['chin', 'engl', 'index', 'school']、含义:['语文', '英语', '学生id', '学校']; 2. 表名:science、字段名:['chem', 'index', 'math', 'phys', 'school']、含义:['化学', '学生id', '数学', '物理', '学校'];

好的,我已经记住了这两张表及其字段含义:

  1. 表名: language,字段名:['chin', 'engl', 'index', 'school'],含义:['语文', '英语', '学生id', '学校'];
  2. 表名: science,字段名:['chem', 'index', 'math', 'phys', 'school'],含义:['化学', '学生id', '数学', '物理', '学校']。
Avatar of the person chatting

查询学校为zjjt的前10个学生的总成绩,并且用pyecharts画一个折线图

假设你使用的数据库是MySQL,那么可以按照以下步骤进行查询和绘制折线图:

  1. 查询学校为zjjt的前10个学生的总成绩:
vbnet
SELECT language.chin + science.chem + science.math + science.phys AS total_score FROM language JOIN science ON language.index = science.index AND language.school = science.school WHERE language.school = 'zjjt' ORDER BY total_score DESC LIMIT 10;
  1. 使用Python连接MySQL数据库,并将查询结果存储到一个列表中:
python
import pymysql # 建立连接 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8mb4') cursor = conn.cursor() # 执行查询语句 cursor.execute('上面的SQL查询语句') results = cursor.fetchall() # 将结果存储到一个列表中 scores = [result[0] for result in results] # 关闭连接 cursor.close() conn.close()
  1. 使用pyecharts绘制折线图:
scss
from pyecharts.charts import Line from pyecharts import options as opts line = Line() line.add_xaxis(list(range(1, 11))) line.add_yaxis('总成绩', scores) line.set_global_opts( title_opts=opts.TitleOpts(title='前10个学生总成绩折线图'), xaxis_opts=opts.AxisOpts(name='排名'), yaxis_opts=opts.AxisOpts(name='总成绩'), ) line.render('line_chart.html')

运行完以上代码后,将生成一个名为line_chart.html的文件,用浏览器打开即可看到绘制出来的折线图。

Avatar of the person chatting

3 / 3查询语文成绩最高的5个学校,并用这个SQL结果画一个饼图

假设你使用的数据库是MySQL,那么可以按照以下步骤进行查询和绘制饼图:

  1. 查询语文成绩最高的5个学校及其对应的成绩:
sql
SELECT language.school, MAX(language.chin) AS max_chin FROM language GROUP BY language.school ORDER BY max_chin DESC LIMIT 5;
  1. 使用Python连接MySQL数据库,并将查询结果存储到两个列表中,一个存储学校名称,另一个存储对应的成绩:
scss
import pymysql # 建立连接 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8mb4') cursor = conn.cursor() # 执行查询语句 cursor.execute('上面的SQL查询语句') results = cursor.fetchall() # 将结果存储到两个列表中 schools = [result[0] for result in results] scores = [result[1] for result in results] # 关闭连接 cursor.close() conn.close()
  1. 使用pyecharts绘制饼图:
python
from pyecharts.charts import Pie from pyecharts import options as opts pie = Pie() pie.add('', [(school, score) for school, score in zip(schools, scores)]) pie.set_global_opts( title_opts=opts.TitleOpts(title='语文成绩最高的5个学校'), legend_opts=opts.LegendOpts(type_='scroll', pos_right='10%'), ) pie.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}: {c}')) pie.render('pie_chart.html')

运行完以上代码后,将生成一个名为pie_chart.html的文件,用浏览器打开即可看到绘制出来的饼图。