python中pandas模块(merge方法)实现SQL Server中的表关联join

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yupeigu/article/details/79230428

python连接sql server的方法,可以参考这篇文章:python连接SQL Server:Pymssql模块

基于下面的数据,分别用sql 和 python,实现数据集的关联,得到 学生、课程、成绩 的数据。

建表

--学生 
CREATE TABLE student    
(    
      sid INT ,    
      sname NVARCHAR(32) ,    
      sage INT ,    
      ssex NVARCHAR(8)    
 )     
    
 --课程表    
CREATE TABLE course    
(    
      cid INT ,    
      cname NVARCHAR(32) ,    
      tid INT    
)     
    
--成绩表    
CREATE TABLE sc ( s# INT, c# INT, score INT )     
    
CREATE TABLE teacher    
(    
      tid INT ,    
      tname NVARCHAR(16)    
)     
    
 insert into Student select 1,N'刘一',18,N'男' union all    
 select 2,N'钱二',19,N'女' union all    
 select 3,N'张三',17,N'男' union all    
 select 4,N'李四',18,N'女' union all    
 select 5,N'王五',17,N'男' union all    
 select 6,N'赵六',19,N'女'     
     
 insert into Teacher     
 select 1,N'叶平' union all    
 select 2,N'贺高' union all    
 select 3,N'杨艳' union all    
 select 4,N'周磊'    
     
 insert into Course     
 select 1,N'语文',1 union all    
 select 2,N'数学',2 union all    
 select 3,N'英语',3 union all    
 select 4,N'物理',4    
     
 insert into SC     
 select 1,1,56 union all     
 select 1,2,78 union all     
 select 1,3,67 union all     
 select 1,4,58 union all     
 select 2,1,79 union all     
 select 2,2,81 union all     
 select 2,3,92 union all     
 select 2,4,68 union all     
 select 3,1,91 union all     
 select 3,2,47 union all     
 select 3,3,88 union all     
 select 3,4,56 union all     
 select 4,2,88 union all     
 select 4,3,90 union all     
 select 4,4,93 union all     
 select 5,1,46 union all     
 select 5,3,78 union all     
 select 5,4,53 union all     
 select 6,1,35 union all     
 select 6,2,68 union all     
 select 6,4,71    

一、SQL Server中的表关联 join

用sql来实现,非常简单,直接用join把3个表关联起来就可以了:

 SELECT s.sname,
        c.cname,
		sc.score
 FROM sc 
 INNER JOIN student s
 ON sc.sid = s.sid
 INNER JOIN course c
 ON sc.cid = c.cid

二、python中的集合关联

# -*- coding:gbk -*-

import pymssql as pm
from pandas import DataFrame #行列结构
import pandas as pd

conn = pm.connect(host='localhost',user='sa',password='momogua',database='test')

cur = conn.cursor()

#获取sc表的数据,格式化
cur.execute('select * from sc')
sc = cur.fetchall()
dsc = DataFrame(sc,columns = ['sid','cid','score'])

#获取student表的数据,格式化
cur.execute('select sid,sname from student')
student = cur.fetchall()
dstudent = DataFrame(student,columns=['sid','sname'])

#获取course表的数据,格式化
cur.execute('select cid,cname from course')
course = cur.fetchall()
dcourse = DataFrame(course,columns=['cid','cname'])

#print dsc
#print dstudent
#print dcourse

#合并sc表与student表
sc_student = pd.merge(dsc,dstudent, left_on='sid',right_on='sid',how='inner')

#在把合并的结果,和course表合并
sc_student_course = pd.merge(sc_student,dcourse,left_on='cid',right_on='cid',how='inner')

#取sname,cname,score 3列
r = sc_student_course[['sname','cname','score']]

print r

输出结果:

>>> ================================ RESTART ================================
>>> 
   sname cname  score
0     刘一    语文     56
1     钱二    语文     79
2     张三    语文     91
3     王五    语文     46
4     赵六    语文     35
5     刘一    数学     78
6     钱二    数学     81
7     张三    数学     47
8     李四    数学     88
9     赵六    数学     68
10    刘一    英语     67
11    钱二    英语     92
12    张三    英语     88
13    李四    英语     90
14    王五    英语     78
15    刘一    物理     58
16    钱二    物理     68
17    张三    物理     56
18    李四    物理     93
19    王五    物理     53
20    赵六    物理     71

猜你喜欢

转载自blog.csdn.net/yupeigu/article/details/79230428
今日推荐