zoukankan      html  css  js  c++  java
  • SqlAlchenmy基本使用

       #简单查询
        print(session.query(User).all())
        print(session.query(User.name, User.fullname).all())
        print(session.query(User, User.name).all())
        
        #带条件查询
        print(session.query(User).filter_by(name='user1').all())
        print(session.query(User).filter(User.name == "user").all())
        print(session.query(User).filter(User.name.like("user%")).all())
        
        #多条件查询
        print(session.query(User).filter(and_(User.name.like("user%"), User.fullname.like("first%"))).all())
        print(session.query(User).filter(or_(User.name.like("user%"), User.password != None)).all())
        
        #sql过滤
        print(session.query(User).filter("id>:id").params(id=1).all())
        
        #关联查询 
        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())
        
        #聚合查询
        print(session.query(User.name, func.count('*').label("user_count")).group_by(User.name).all())
        print(session.query(User.name, func.sum(User.id).label("user_id_sum")).group_by(User.name).all())
        
        #子查询
        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).outerjoin((stmt, User.id == stmt.c.user_id)).order_by(User.id).all())
        
        #exists
        print(session.query(User).filter(exists().where(Address.user_id == User.id)))
        print(session.query(User).filter(User.addresses.any()))

    多条件查询例子:

    from sqlalchemy import and_
    words = ['%三%', '%猫%']
    rule = and_(*[table.u_name.like(w) for w in words])
    table.query.filter(rule)

    限制返回字段查询

    person = session.query(Person.name, Person.created_at,                     
                 Person.updated_at).filter_by(name="zhongwei").order_by(            
                 Person.created_at).first()
    

    记录总数查询:

    from sqlalchemy import func
    
    # count User records, without
    # using a subquery.
    session.query(func.count(User.id))
    
    # return count of user "id" grouped
    # by "name"
    session.query(func.count(User.id)).
            group_by(User.name)
    
    from sqlalchemy import distinct
    
    # count distinct "name" values
    session.query(func.count(distinct(User.name)))
    

      

    Sqlalchemy查询的like mysql order by desc 倒叙排例子

      

    res  =Session.query(OnlineHistoryInfo).order_by(OnlineHistoryInfo.id.desc()).all()
    
    for i in res:
        print (i.task_run_user,i.id)
    

     

    #网上的例子
    query = (model.Session.query(model.Entry) .join(model.ClassificationItem) .join(model.EnumerationValue) .filter_by(id=c.row.id) .order_by(model.Entry.amount.desc()) )

      

  • 相关阅读:
    【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
    【BZOJ】1708: [Usaco2007 Oct]Money奶牛的硬币
    【BZOJ】1690: [Usaco2007 Dec]奶牛的旅行
    【BZOJ】1725: [Usaco2006 Nov]Corn Fields牧场的安排
    【BZOJ】1711: [Usaco2007 Open]Dining吃饭
    【BZOJ】1579: [Usaco2009 Feb]Revamping Trails 道路升级
    【BZOJ】1572: [Usaco2009 Open]工作安排Job
    【51NOD】1717 好数
    【POJ】3233 Matrix Power Series
    【BZOJ】1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
  • 原文地址:https://www.cnblogs.com/liujiliang/p/8583694.html
Copyright © 2011-2022 走看看