原文链接:https://my.oschina.net/freegeek/blog/222725
几种常见sqlalchemy查询:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<code class = "hljs python" > <span class = "hljs-comment" > #简单查询 print (session.query(User). all ()) print (session.query(User.name, User.fullname). all ()) print (session.query(User, User.name). all ()) <span class = "hljs-comment" > #带条件查询 print (session.query(User).filter_by(name = <span class = "hljs-string" > 'user1' ). all ()) print (session.query(User). filter (User.name = = <span class = "hljs-string" > "user" ). all ()) print (session.query(User). filter (User.name.like(<span class = "hljs-string" > "user%" )). all ()) <span class = "hljs-comment" > #多条件查询 print (session.query(User). filter (and_(User.name.like(<span class = "hljs-string" > "user%" ), User.fullname.like(<span class = "hljs-string" > "first%" ))). all ()) print (session.query(User). filter (or_(User.name.like(<span class = "hljs-string" > "user%" ), User.password ! = <span class = "hljs-keyword" > None )). all ()) <span class = "hljs-comment" > #sql过滤 print (session.query(User). filter (<span class = "hljs-string" > "id>:id" ).params( id = <span class = "hljs-number" > 1 ). all ()) <span class = "hljs-comment" > #关联查询 print (session.query(User, Address). filter (User. id = = Address.user_id). all ()) print (session.query(User).join(User.addresses). all ()) print (session.query(User).outerjoin(User.addresses). all ()) <span class = "hljs-comment" > #聚合查询 print (session.query(User.name, func.count(<span class = "hljs-string" > '*' ).label(<span class = "hljs-string" > "user_count" )).group_by(User.name). all ()) print (session.query(User.name, func. sum (User. id ).label(<span class = "hljs-string" > "user_id_sum" )).group_by(User.name). all ()) <span class = "hljs-comment" > #子查询 stmt = session.query(Address.user_id, func.count(<span class = "hljs-string" > '*' ).label(<span class = "hljs-string" > "address_count" )).group_by(Address.user_id).subquery() print (session.query(User, stmt.c.address_count).outerjoin((stmt, User. id = = stmt.c.user_id)).order_by(User. id ). all ()) <span class = "hljs-comment" > #exists print (session.query(User). filter (exists().where(Address.user_id = = User. id ))) print (session.query(User). filter (User.addresses. any ()))< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / code> |
限制返回字段查询
1
2
3
|
<code class = "hljs nginx" ><span class = "hljs-attribute" >person = session.query(Person.name, Person.created_at, Person.updated_at).filter_by(name = <span class = "hljs-string" > "zhongwei" ).order_by( Person.created_at).first()< / span>< / span>< / code> |
记录总数查询:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<code class = "hljs swift" > from sqlalchemy <span class = "hljs-keyword" > import func # <span class="hljs-built_in">count <span class="hljs-type">User records, without # using a subquery. session.query(<span class = "hljs-function" ><span class = "hljs-keyword" ><span class = "hljs-function" ><span class = "hljs-keyword" >func<span class = "hljs-function" >.<span class = "hljs-title" ><span class = "hljs-function" ><span class = "hljs-title" >count<span class = "hljs-params" ><span class = "hljs-function" ><span class = "hljs-params" >(User. id )) # <span class="hljs-keyword">return <span class="hljs-built_in">count of user <span class="hljs-string">"id" grouped # by <span class="hljs-string">"name" session.query(<span class = "hljs-function" ><span class = "hljs-keyword" ><span class = "hljs-function" ><span class = "hljs-keyword" >func<span class = "hljs-function" >.<span class = "hljs-title" ><span class = "hljs-function" ><span class = "hljs-title" >count<span class = "hljs-params" ><span class = "hljs-function" ><span class = "hljs-params" >(User. id )). group_by(<span class = "hljs-type" >User.name) from sqlalchemy <span class = "hljs-keyword" > import distinct # <span class="hljs-built_in">count distinct <span class="hljs-string">"name" values session.query(<span class = "hljs-function" ><span class = "hljs-keyword" ><span class = "hljs-function" ><span class = "hljs-keyword" >func<span class = "hljs-function" >.<span class = "hljs-title" ><span class = "hljs-function" ><span class = "hljs-title" >count<span class = "hljs-params" ><span class = "hljs-function" ><span class = "hljs-params" >(distinct<span class = "hljs-params" ><span class = "hljs-function" ><span class = "hljs-params" ><span class = "hljs-params" >(User.name)))< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / span>< / code> |