首頁 >資料庫 >mysql教程 >Python中使用SQLAlchemy连接Mysql数据库2(多表连接操作)

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

WBOY
WBOY原創
2016-06-07 14:51:112090瀏覽

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
在上面我们已经说了单表的一些操作,现在我们说一下多表的连接

<code class=" hljs python"><span class="hljs-prompt">>>> </span><span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> distinct
<span class="hljs-prompt">>>> </span><span class="hljs-keyword">from</span> sqlalchemy.orm <span class="hljs-keyword">import</span> aliased
<span class="hljs-prompt">>>> </span>Astu = aliased(Stu,<span class="hljs-string">'Astu'</span>)
<span class="hljs-prompt">>>> </span>Acla = aliased(Cla,<span class="hljs-string">'Acla'</span>)
<span class="hljs-prompt">>>> </span>Agrade = aliased(Grade,<span class="hljs-string">'Agrade'</span>)</code>

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

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

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

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

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

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

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

<code class=" hljs asciidoc"><span class="hljs-header">mysql> select * from grade where gre=100;
+----+------+------+------+</span>
<span class="hljs-header">| id | uid  | cid  | gre  |
+----+------+------+------+</span>
<span class="hljs-header">| 15 |    2 |    3 |  100 |
+----+------+------+------+</span>
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})
</code>

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

<code class=" hljs asciidoc">#用SQL语句把学语文的成绩都改为110
>>> session.execute("update grade set gre=110 where cid = (select id from cla ere cname=<span class="hljs-emphasis">'yuwen'</span>)")
<span class="hljs-code"> update grade set gre=110 where cid = (select id from cla where cname='yuwen')</span>
2016-05-10 17:15:59,383 INFO sqlalchemy.engine.base.Engine ()
<sqlalchemy.engine.result.ResultProxy object at 0xb5c895cc>
<span class="hljs-header">mysql> select * from grade where cid =(select id from cla where cname='yuwen');
+----+------+------+------+</span>
<span class="hljs-header">| id | uid  | cid  | gre  |
+----+------+------+------+</span>
|  2 |    2 |    1 |   66 |
<span class="hljs-header">|  3 |    5 |    1 |   66 |
+----+------+------+------+</span>
2 rows in set (0.00 sec)
#提交后
>>> session.commit()
2016-05-10 17:16:18,223 INFO sqlalchemy.engine.base.Engine COMMIT
>>> 
<span class="hljs-header">mysql> select * from grade where cid =(select id from cla where cname='yuwen');
+----+------+------+------+</span>
<span class="hljs-header">| id | uid  | cid  | gre  |
+----+------+------+------+</span>
|  2 |    2 |    1 |  110 |
<span class="hljs-header">|  3 |    5 |    1 |  110 |
+----+------+------+------+</span>
2 rows in set (0.00 sec)</code>
<code class=" hljs avrasm"><span class="hljs-preprocessor">#删除成绩为50的成绩记录</span>
>>> session<span class="hljs-preprocessor">.query</span>(Grade)<span class="hljs-preprocessor">.filter</span>(Grade<span class="hljs-preprocessor">.gre</span>==<span class="hljs-number">50</span>)<span class="hljs-preprocessor">.delete</span>()</code>
<code class=" hljs asciidoc">#删除选修英语的所有成绩记录,用SQL进行
>>> session.execute( "delete from grade where cid =(select id from cla where cme=<span class="hljs-emphasis">'yingyu'</span>)")
2016-05-10 16:55:55,472 INFO sqlalchemy.engine.base.Engine delete from grade where cid =(select id from cla where cname=<span class="hljs-emphasis">'yingyu'</span>)
2016-05-10 16:55:55,472 INFO sqlalchemy.engine.base.Engine ()
mysql> select * from grade where cid = (select id from cla where cname=<span class="hljs-emphasis">'yingyu'</span>)
<span class="hljs-header">    -> ;
+----+------+------+------+</span>
<span class="hljs-header">| id | uid  | cid  | gre  |
+----+------+------+------+</span>
|  5 |    5 |    3 |   96 |
|  9 |    4 |    3 |   76 |
<span class="hljs-header">| 14 |    3 |    3 |   60 |
+----+------+------+------+</span>
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=<span class="hljs-emphasis">'yingyu'</span>)
<span class="hljs-code">    -> ;</span>
Empty set (0.00 sec)</code>

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn