SQLALchemy-查询篇
程序员文章站
2024-03-03 16:28:16
...
似乎ORM最难设计的部分是查询。特别是面向对象的查询,今天学习SQLAlchemy,发现SQLAlchemy的查询语法竟如此灵活,惊叹其如此强大的表达能力的同时也对Python也有了更深的认识。下面看一下我写的一些查询语句:
Python代码:
简单查询
注意User是一个类对象,user_table是数据库中的表
print(session.query(User).all())
—相当于SQL语句—>
select * from user_table
print(session.query(User.name,User.fullname.all()))
—相当于SQL语句—>
select user_table.name,user_table.fullname from user_table
print(session.query(User,user.name).all())
—相当于SQL语句—>
select user_table.name from user_table
条件查询
print (session.query(User).filter_by(name=’user1).all())
—相当于SQL语句—>
select * from user_table where name = ‘user1’
print (session.query(User).filter(User.name == “user).all())
—相当于SQL语句—>
select * from user_table where user_table.name = user
print (session.query(User).filter(User.name.like(“user%”)).all())
—相当于SQL语句—>
select * from user_table where user_table.name like user%
多条件查询
print (session.query(User).filter(and_(User.name.like(“user),User.fullname.like(“first%”))).all()
—相当于SQL语句—>
select * from user_table where user_table.name like %user and user_table.fullname like first%
print(session.query(User).filter(or_(User.name.like(“user%),User.password != None)).all()
—相当于SQL语句—>
select * from user_table where user_table.name = user% or user_table.password != none
sql过滤
print(session(User).filter(“id>:id”).params(id=1).all()
—相当于SQL语句—>
select * from user_table where user_table.id > 1
关联查询
print(session.query(User,Address).filter(User.id == Address.user_id).all()
—相当于SQL语句—>
select * from user_table,address_table where user_table.id == address.user_id
print (session.query(User).jion(User.address).all()
—相当于SQL语句—>
print (session.query(User).outerjoin(User.address).all())
聚合查询
print(session.query(User.name,func.count(‘*’).label(“user_count)).group_by(User.name).all())
—相当于SQL语句—>
select count(user_table.name) as user_count from user_table where group by(user_table.name)
print(session.query(User.name,func.sum(User.id).label(“user_id_sum”)).group_by(User.name).all())
—相当于SQL语句—>
select user_table.name,sum(user_table.id) from user_table where group by(user_table.name)
子查询
stmt = session.query(Address.user_id,func.count(‘*’).label(“address_count”).group by(Address.user_id).subquery()
print (session.query(User,stmt.c.address_count).outjion((stmt,User.id == stmt.c.user_id.order_by(User_id).all()
exits
print (session.query(User).filter(exists().where(Address.user_id == User.id)))
print (session.query(User).filter(User.addresses.any()))
上一篇: python利用正则表达式排除集合中字符的功能示例
下一篇: PyParsec 升级至 0.7.3