当前位置:Gxlcms > mysql > Python中使用SQLAlchemy连接Mysql数据库2(多表连接操作)

Python中使用SQLAlchemy连接Mysql数据库2(多表连接操作)

时间:2021-07-01 10:21:17 帮助过:15人阅读

http://blog.csdn.net/u011573853/article/details/51355113 在上面我们已经说了单表的一些操作,现在我们说一下多表的连接 from sqlalchemy import distinct from sqlalchemy.orm import aliased Astu = aliased(Stu, 'Astu' ) Acla = aliased(Cla, 'Acla'

http://blog.csdn.net/u011573853/article/details/51355113
在上面我们已经说了单表的一些操作,现在我们说一下多表的连接

>>> from sqlalchemy import distinct
>>> from sqlalchemy.orm import aliased
>>> Astu = aliased(Stu,'Astu')
>>> Acla = aliased(Cla,'Acla')
>>> Agrade = aliased(Grade,'Agrade')

在这里先用函数aliased()给表起一个别名以备使用

#查询所有有成绩的学号
>>> query = session.query(Stu).join(Grade,Stu.id==Grade.uid).all()
SELECT stu.id AS stu_id, stu.name AS stu_name 
FROM stu INNER JOIN grade ON stu.id = grade.uid
2016-05-10 15:02:36,476 INFO sqlalchemy.engine.base.Engine ()
>>> for re in query:
...     print re.id
... 
1
2
3
4
5
>>> 
#查找有成绩的同学的学号好成绩
>>> print session.query(Grade.uid,Grade.gre).join(Stu,Grade.uid==Stu.id).all()
SELECT grade.uid AS grade_uid, grade.gre AS grade_gre 
FROM grade INNER JOIN stu ON grade.uid = stu.id
2016-05-10 15:15:22,208 INFO sqlalchemy.engine.base.Engine ()
[(1L, 76L), (1L, 76L), (2L, 66L), (2L, 76L), (2L, 50L), (3L, 96L), (3L, 76L), (3L, 60L), (4L, 76L), (4L, 76L), (4L, 76L), (5L, 66L), (5L, 96L), (5L, 96L)]
>>> 
#查找有成绩同学的学好成绩和课程名
>>>print session.query(Grade.uid,Grade.gre,Cla.cname).join(Stu,Grade.uid==Stuid).join(Cla,Grade.cid==Cla.id).all()
 SELECT grade.uid AS grade_uid, grade.gre AS grade_gre, cla.cname AS cla_cname 
FROM grade INNER JOIN stu ON grade.uid = stu.id INNER JOIN cla ON grade.cid = cla.id
2016-05-10 15:21:29,402 INFO sqlalchemy.engine.base.Engine ()
[(2L, 66L, 'yuwen'), (5L, 66L, 'yuwen'), (3L, 96L, 'shuxue'), (2L, 50L, 'yingyu'), (3L, 60L, 'yingyu'), (4L, 76L, 'yingyu'), (5L, 96L, 'yingyu'), (1L, 76L, 'wuli'), (3L, 76L, 'wuli'), (4L, 76L, 'wuli'), (1L, 76L, 'huaxue'), (2L, 76L, 'huaxue'), (4L, 76L, 'huaxue'), (5L, 96L, 'huaxue')]
>>> 
#查找有成绩同学的学好成绩和课程名,并进行排序
>>> print session.query(Grade.uid,Grade.gre,Cla.cname).join(Stu,Grade.uid==Stud).join(Cla,Grade.cid==Cla.id).order_by(Grade.uid,Grade.gre.desc()).all()
SELECT grade.uid AS grade_uid, grade.gre AS grade_gre, cla.cname AS cla_cname 
FROM grade INNER JOIN stu ON grade.uid = stu.id INNER JOIN cla ON grade.cid = cla.id 
ORDER BY grade.uid, grade.gre DESC
2016-05-10 15:34:17,902 INFO sqlalchemy.engine.base.Engine ()
[(1L, 76L, 'wuli'), (1L, 76L, 'huaxue'), (2L, 76L, 'huaxue'), (2L, 66L, 'yuwen'), (2L, 50L, 'yingyu'), (3L, 96L, 'shuxue'), (3L, 76L, 'wuli'), (3L, 60L, 'yingyu'), (4L, 76L, 'yingyu'), (4L, 76L, 'huaxue'), (4L, 76L, 'wuli'), (5L, 96L, 'yingyu'), (5L, 96L, 'huaxue'), (5L, 66L, 'yuwen')]
>>> 

>>> query2=session.query(Grade.uid,Grade.gre,Cla.cname).join(Stu,Grade.uid==Stid).join(Cla,Grade.cid==Cla.id).order_by(Grade.uid,Grade.gre.desc()).all()
SELECT grade.uid AS grade_uid, grade.gre AS grade_gre, cla.cname AS cla_cname 
FROM grade INNER JOIN stu ON grade.uid = stu.id INNER JOIN cla ON grade.cid = cla.id ORDER BY grade.uid, grade.gre DESC
2016-05-10 15:37:33,628 INFO sqlalchemy.engine.base.Engine ()
>>> for re in query2:
...     print re.uid,re.gre,re.cname
... 
1 76 wuli
1 76 huaxue
2 76 huaxue
2 66 yuwen
2 50 yingyu
3 96 shuxue
3 76 wuli
3 60 yingyu
4 76 yingyu
4 76 huaxue
4 76 wuli
5 96 yingyu
5 96 huaxue
5 66 yuwen
>>> 

只需要把你要查的字段写在query()中就好了

#查找成绩小于等于70同学的学好成绩和课程名,并进行排序
>>>print  session.query(Grade.uid,Grade.gre,Cla.cname).join(Stu,Grade.uid==Stu.id).jn(Cla,Grade.cid==Cla.id).filter(Grade.gre<=70).order_by(Grade.uid,Grade.gre.de()).all()
SELECT grade.uid AS grade_uid, grade.gre AS grade_gre, cla.cname AS cla_cname 
FROM grade INNER JOIN stu ON grade.uid = stu.id INNER JOIN cla ON grade.cid = cla.id 
WHERE grade.gre <= %s ORDER BY grade.uid, grade.gre DESC
2016-05-10 15:42:06,742 INFO sqlalchemy.engine.base.Engine (70,)
[(2L, 66L, 'yuwen'), (2L, 50L, 'yingyu'), (3L, 60L, 'yingyu'), (5L, 66L, 'yuwen')]
>>> 

#用SQL语言来查询
>>> query = session.execute("select uid from grade where cid = (select id fromla where cname='yuwen')")
select uid from grade where cid = (select id from cla where cname='yuwen')
2016-05-10 17:12:57,395 INFO sqlalchemy.engine.base.Engine ()
>>> for re in query:
...     print re.uid
... 
2
5
>>> 
#把学好为2成绩为100的学生成绩改为99
>>> query5 = session.query(Grade)
>>> query5.filter(Grade.uid==2,Grade.gre==100).update({Grade.gre:99})
2016-05-10 16:28:46,485 INFO sqlalchemy.engine.base.Engine UPDATE grade SET gre=%s WHERE grade.uid = %s AND grade.gre = %s
2016-05-10 16:28:46,486 INFO sqlalchemy.engine.base.Engine (99, 2, 100)
1L

此时没有提交还没有正在的改变

mysql> select * from grade where gre=100;
+----+------+------+------+
| id | uid  | cid  | gre  |
+----+------+------+------+
| 15 |    2 |    3 |  100 |
+----+------+------+------+
1 row in set (0.00 sec)

>>> session.commit()
mysql> select * from grade where gre=100;
Empty set (0.00 sec)

也可以这样搞
>>> session.query(Grade).filter(Grade.gre==99).update({Grade.gre:50})

update里面使用的是字典类型
或这样搞

#用SQL语句把学语文的成绩都改为110
>>> session.execute("update grade set gre=110 where cid = (select id from cla ere cname='yuwen')")
 update grade set gre=110 where cid = (select id from cla where cname='yuwen')
2016-05-10 17:15:59,383 INFO sqlalchemy.engine.base.Engine ()

mysql> select * from grade where cid =(select id from cla where cname='yuwen');
+----+------+------+------+
| id | uid  | cid  | gre  |
+----+------+------+------+
|  2 |    2 |    1 |   66 |
|  3 |    5 |    1 |   66 |
+----+------+------+------+
2 rows in set (0.00 sec)
#提交后
>>> session.commit()
2016-05-10 17:16:18,223 INFO sqlalchemy.engine.base.Engine COMMIT
>>> 
mysql> select * from grade where cid =(select id from cla where cname='yuwen');
+----+------+------+------+
| id | uid  | cid  | gre  |
+----+------+------+------+
|  2 |    2 |    1 |  110 |
|  3 |    5 |    1 |  110 |
+----+------+------+------+
2 rows in set (0.00 sec)
#删除成绩为50的成绩记录
>>> session.query(Grade).filter(Grade.gre==50).delete()
#删除选修英语的所有成绩记录,用SQL进行
>>> session.execute( "delete from grade where cid =(select id from cla where cme='yingyu')")
2016-05-10 16:55:55,472 INFO sqlalchemy.engine.base.Engine delete from grade where cid =(select id from cla where cname='yingyu')
2016-05-10 16:55:55,472 INFO sqlalchemy.engine.base.Engine ()
mysql> select * from grade where cid = (select id from cla where cname='yingyu')
    -> ;
+----+------+------+------+
| id | uid  | cid  | gre  |
+----+------+------+------+
|  5 |    5 |    3 |   96 |
|  9 |    4 |    3 |   76 |
| 14 |    3 |    3 |   60 |
+----+------+------+------+
3 rows in set (0.00 sec)

>>> session.commit()
2016-05-10 16:56:33,075 INFO sqlalchemy.engine.base.Engine COMMIT
>>> 

mysql> select * from grade where cid = (select id from cla where cname='yingyu')
    -> ;
Empty set (0.00 sec)

级联删除:在relationship关联时要加上passive_deletes=True外键要加上ondelete=’CASCADE’,否则sqlalchemy不能级联删除
这些是多表连接的操作
单表的操作请看http://blog.csdn.net/u011573853/article/details/51355113
一些细节会在下面进行说明(事务,加锁,编码等)
http://blog.csdn.net/u011573853/article/details/51366124

人气教程排行