温故知新(下):MySQL vs Python

Posted INNER JOIN

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了温故知新(下):MySQL vs Python相关的知识,希望对你有一定的参考价值。

问题4:

1.查询选修01课程的学生信息

2.查询选修语文课程的学生信息

3.查询出只选修两门课程的学生学号和姓名

4.查询学生的基本信息以及该生选修了多少门课程


一、mysql方法

SELECT

 student.*

FROM student INNER JOIN sc ONstudent.Sno=sc.Sno

 INNER JOIN course ON sc.Cno=course.Cno

WHERE course.Cno="01"

 

SELECT

 student.*

FROM student INNER JOIN sc ONstudent.Sno=sc.Sno

 INNER JOIN course ON sc.Cno=course.Cno

WHERE course.Cname="语文"

 

SELECT

 student.Sno,

 student.Sname

FROM student INNER JOIN sc ONstudent.Sno=sc.Sno

GROUP BY student.Sno, student.Sname

HAVING COUNT(sc.Cno)=2

 

SELECT

 sc.Sno,

 COUNT(sc.Cno)AS 课程数

FROM sc

GROUP BY sc.Sno

 

SELECT

  *

FROM student 

  INNER JOIN new_course onstudent.Sno=new_course.Sno

 

二、Python方法

import pandas as pd

 

pd.options.display.max_columns = 999

pd.options.display.max_rows = 999

 

student = pd.read_excel('C:/Liu/Test/School.xlsx',sheet_name='Student', dtype={'Sno': str, 'Class': str})

teacher =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Teacher', dtype={'Tno':str})

course =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Course', dtype={'Cno':str, 'Tno': str})

score =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='SC', dtype={'Sno': str,'Cno': str})

 

full = student.merge(score,on='Sno').merge(course, on='Cno')

 

qT1 =full.query("Cno=='01'")[['Sno', 'Sname', 'Sbirthday', 'Ssex', 'Class']]

print(qT1)

 

qT2 = full.query("Cname=='语文'")[['Sno', 'Sname', 'Sbirthday', 'Ssex', 'Class']]

print(qT2)

 

qT3 = full.pivot_table(index=['Sno','Sname'], values='Cno', aggfunc='count').reset_index().query('Cno==2')

print(qT3)

 

qT4 = full.pivot_table(index=['Sno','Sname', 'Sbirthday', 'Ssex', 'Class'], values='Cno',aggfunc='count').reset_index()

print(qT4)


问题5:

1.查询数学成绩小于60分的记录,字段:学号、姓名、课程号、课程名称、成绩

2.查询学过「张三」老师授课的同学的信息

3.查询没有选修任意一门课程的学生的学号、学生姓名

4.查询李四老师授课课程的最高分、最低分

5.查询同样课程比‘02’号同学成绩高的同学的学号,课程号和分数


一、MySQL方法

SELECT

 student.Sno,

 student.Sname,

 course.Cno,

 course.Cname,

 sc.Score

FROM student INNER JOIN sc ONstudent.Sno=sc.Sno

 INNER JOIN course ON sc.Cno=course.Cno

WHERE course.Cname="数学" AND sc.Score<60

 

SELECT

 student.*

FROM student INNER JOIN sc ONstudent.Sno=sc.Sno

 INNER JOIN course ON sc.Cno=course.Cno

       INNERJOIN teacher ON course.Tno=teacher.Tno

WHERE teacher.Tname="张三"

 

SELECT

 student.Sno,

 student.Sname

FROM student LEFT JOIN sc ONstudent.Sno=sc.Sno

WHERE sc.Cno IS NULL

 

SELECT

 MAX(sc.Score) AS 最高分,

 MIN(sc.Score)AS 最低分

FROM sc INNER JOIN course ONsc.Cno=course.Cno

 INNER JOIN teacher ON course.Tno=teacher.Tno

WHERE teacher.Tname="李四"

 

SELECT

  *

FROM sc

WHERE sc.Sno="02"

 

SELECT

  *

FROM sc

WHERE sc.Score>(SELECT new_score.ScoreFROM new_score WHERE new_score.Cno=sc.Cno)

 

二、Python方法

import pandas as pd

 

pd.options.display.max_columns = 999

pd.options.display.max_rows = 999

 

student =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Student', dtype={'Sno':str, 'Class': str})

teacher =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Teacher', dtype={'Tno':str})

course =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Course', dtype={'Cno':str, 'Tno': str})

score = pd.read_excel('C:/Liu/Test/School.xlsx',sheet_name='SC', dtype={'Sno': str, 'Cno': str})

 

full = student.merge(score,on='Sno').merge(course, on='Cno').merge(teacher, on='Tno')

 

qT1 = full.query("Cname=='数学' & Score<60")[['Sno', 'Sname', 'Cno', 'Cname','Score']].reset_index(drop=True)

print(qT1)

 

qT2 = full.query("Tname=='张三'")[['Sno', 'Sname', 'Sbirthday', 'Ssex','Class']].reset_index(drop=True)

print(qT2)

 

qT3 = student.merge(score, how='left',on='Sno')

qT3 = qT3[qT3['Cno'].isnull()][['Sno','Sname']]

print(qT3)

 

max_sc = full.query("Tname=='李四'")['Score'].max()

min_sc = full.query("Tname=='李四'")['Score'].min()

print(max_sc, min_sc)

 

temp = score.query("Sno=='02'")

sample = score.merge(temp, how='left',on='Cno')

qT4 = sample[sample['Score_x'] >sample['Score_y']][['Sno_x', 'Cno', 'Score_x']].reset_index(drop=True)

print(qT4)


问题6:

1.查询Student表中'01'班所有学生的基本信息。

2.查询SC表中成绩不及格的所有记录。

3.查询SC表中成绩在70到80之间的所有记录。

4.查询SC表中'01'号课程成绩在70到80之间的所有记录。

5.查询SC表中课程号为'01'或'01'号学生成绩在70到80之间的所有记录。

6.查询Student表中性别未知的学号、姓名。

7.查询Student表中'01'班性别不为空的所有学生基本信息。

8.查询Student表中不姓“周”的同学记录。

9.查询Student表中姓“王”且在“01”班中的同学记录

10.查询SC表里分数为60、70或80的学生的学号、课程号和对应分数。


一、MySQL方法

SELECT

  *

FROM student

WHERE student.Class="01"

 

SELECT

  *

FROM sc

WHERE sc.Score<60

 

SELECT

  *

FROM sc

WHERE sc.Score  BETWEEN 70 AND 80

 

SELECT

  *

FROM sc

WHERE (sc.Cno="01") AND(sc.Score  BETWEEN 70 AND 80)

 

SELECT

  *

FROM sc

WHERE (sc.Cno="01" ORsc.Sno="01") AND (sc.Score BETWEEN 70 AND 80)

 

SELECT

 student.Sno,

       student.Sname

FROM student

WHERE student.Ssex="未知"

 

SELECT

  *

FROM student

WHERE student.Class="01" ANDstudent.Ssex IS NOT NULL

 

SELECT

  *

FROM student

WHERE student.Sname NOT LIKE "周%"

 

SELECT

  *

FROM student

WHERE student.Class="01" ANDstudent.Sname LIKE "王%"

 

 

SELECT

  *

FROM sc

WHERE sc.Score in (60,70,80)

 

二、Python方法

import pandas as pd

 

pd.options.display.max_columns = 999

pd.options.display.max_rows = 999

 

student =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Student', dtype={'Sno':str, 'Class': str})

teacher =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Teacher', dtype={'Tno':str})

course =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='Course', dtype={'Cno':str, 'Tno': str})

score =pd.read_excel('C:/Liu/Test/School.xlsx', sheet_name='SC', dtype={'Sno': str,'Cno': str})

 

qT1 =student.query("Class=='01'").reset_index(drop=True)

print(qT1)

 

qT2 =score.query('Score<60').reset_index(drop=True)

print(qT2)

 

qT3 = score.query('70<=Score<=80').reset_index(drop=True)

print(qT3)

 

qT4 =score.query("(Cno=='01')&(70<=Score<=80)").reset_index(drop=True)

print(qT4)

 

qT5 =score.query("((Cno=='01')|(Sno=='01'))&(70<=Score<=80)").reset_index(drop=True)

print(qT5)

 

qT6 = student[student['Ssex'].isnull()][['Sno','Sname']].reset_index(drop=True)

print(qT6)

 

qT7 = student[(student['Ssex'].notnull())& (student['Class'] == '01')].reset_index(drop=True)

print(qT7)

 

qT8 =student[~student['Sname'].str.contains('周')].reset_index(drop=True)

print(qT8)

 

qT9 =student[(student['Sname'].str.contains('王')) &(student['Class'] == '01')].reset_index(drop=True)

print(qT9)

 

qT10 = score.query('Score in(60,70,80)').reset_index(drop=True)

print(qT10)


以上是关于温故知新(下):MySQL vs Python的主要内容,如果未能解决你的问题,请参考以下文章

黄埔计划Python-温故知新

mysql温故知新

温故而知新MYSQL

mysql 几种索引的复习(温故而知新)

温故而知新-mysql的一些语法show,describe,explain,fulltext

MySQL 温故而知新--Innodb存储引擎中的锁