• 9、ORM框架------SQLAlchemy(2)


    #条件
    from sqlalchemy import and_,or_
    res1 = sess.query(UserType).filter(UserType.id > 2,UserType.title=='超级').all()   # filter内用','隔开,表示and关系
    res2 = sess.query(UserType).filter(UserType.id.between(1,3)).all()  # between表示范围是[1,3]
    res3 = sess.query(UserType).filter(UserType.id.in_([1,4])).all()  # in_表示1,4
    res4 = sess.query(UserType).filter(~UserType.id.in_([1,4])).all()  # ~ 表示非即not
    res5 = sess.query(UserType).filter(UserType.id.in_(sess.query(UserType.id).filter(UserType.title != '黑金'))) #临时表
    res6 = sess.query(UserType).filter(or_(
        UserType.id > 1,
        and_(UserType.id > 2,UserType.title=='白金'), #默认就是and
    ))
    #通配符
    res7 = sess.query(UserType).filter(UserType.title.like('%金')).all()  # like,%
    res8 = sess.query(UserType).filter(~UserType.title.like('_金')).all()  # like,_
    #限制(分页)
    res9 = sess.query(UserType).all()[:2]  #切片
    #排序
    res10 = sess.query(UserType).order_by(UserType.id.desc())
    res11 = sess.query(UserType).order_by(UserType.id.desc(),UserType.title.asc())  #id重名,按title排
    #分组
    from sqlalchemy import func
    res12 = sess.query(User).group_by(User.usertype_id)
    res13 = sess.query(
        func.max(User.id),
        func.min(User.id),
        func.sum(User.id),
        func.avg(User.id),
        func.count(User.id)
    ).group_by(User.usertype_id).having(func.avg(User.id)==2)       #结果是元组
    #连表
    res14 = sess.query(User,UserType).filter(User.usertype_id==UserType.id).all()
    for item in res14:
        print(item[0].id,item[0].name,item[0].usertype_id,item[1].id,item[1].title)
    res15 = sess.query(User,UserType).join(UserType,isouter=True)   #left join
    print(res15)
    for item in res15:
        print(item)
    SELECT user.id AS user_id, user.name AS user_name, user.usertype_id AS user_usertype_id, usertype.id AS usertype_id, usertype.title AS usertype_title 
    FROM user LEFT OUTER JOIN usertype ON usertype.id = user.usertype_id
    (<__main__.User object at 0x000001FC18DCF390>, <__main__.UserType object at 0x000001FC18D9A4A8>)
    (<__main__.User object at 0x000001FC18DCF160>, <__main__.UserType object at 0x000001FC18D9A710>)
    (<__main__.User object at 0x000001FC18D9AB00>, <__main__.UserType object at 0x000001FC18D9A710>)
    (<__main__.User object at 0x000001FC18D9ACC0>, <__main__.UserType object at 0x000001FC18D9A780>)
    #组合
    res7 = sess.query(UserType.id,UserType.title).filter(UserType.title.like('%金'))
    res12 = sess.query(User.id,User.name)
    ret1 = res7.union_all(res12) #去重
    ret2 = res7.union_all(res12) #不去重
    for i,j in zip(ret1,ret2):
        print(i,j)
    (2, '白金') (2, '白金')
    (3, '黑金') (3, '黑金')
    (3, 'ddd') (3, 'ddd')
    (2, 'eee') (2, 'eee')
    (1, 'qqq') (1, 'qqq')
    (4, 'rrr') (4, 'rrr')
    #子查询
    s1 = sess.execute('select * from user WHERE id IN (SELECT id FROM usertype WHERE usertype.title != "超级")')
    for i in s1:
        print(i)
    q1 = sess.query(User.id,User.name,User.usertype_id).filter(User.id.in_(sess.query(UserType.id).filter(UserType.title != '超级')))
    for i in q1:
        print(i)
    
    s2 = sess.execute('select * from (SELECT title from usertype) as u')
    for i in s2:
        print(i)
    q2 = sess.query(UserType.title).subquery()
    r2 = sess.query(q2).all()
    print(r2)
    
    s3 = sess.execute('select *,(SELECT title from usertype where user.usertype_id=usertype.id) from user')
    for i in s3:
        print(i)
    q3 = sess.query(
        User.id,
        User.name,
        User.usertype_id,
        sess.query(UserType.title).filter(UserType.id==User.usertype_id).as_scalar()  #此处不能用subquery()
    )
    for i in q3:
        print(i)
    (1, 'qqq', 2)
    (2, 'eee', 1)
    (3, 'ddd', 2)
    (
    1, 'qqq', 2) (2, 'eee', 1) (3, 'ddd', 2)
    (
    '普通',) ('白金',) ('黑金',) ('超级',)
    [(
    '普通',), ('白金',), ('黑金',), ('超级',)]
    (
    1, 'qqq', 2, '白金') (2, 'eee', 1, '普通') (3, 'ddd', 2, '白金') (4, 'rrr', 4, '超级')
    (
    1, 'qqq', 2, '白金') (2, 'eee', 1, '普通') (3, 'ddd', 2, '白金') (4, 'rrr', 4, '超级')
    # relationship
    
    # from sqlalchemy.orm import relationship   先导入relationship
    # user_type = relationship('UserType',backref='to_user')
    # 在有外键的类中加入自定义字段并使用relationship,第一个参数是外键关联的类名,第二个参数在关联类中也生产一个自定义的字段用于反向操作
    
    #1、查询用户信息及用户类型
    q1 = sess.query(User)
    for i in q1:
        print(i.id,i.name,i.usertype_id,i.user_type.title)
    
    #2、查询用户类型及对应的用户信息
    q2 = sess.query(UserType)
    for i in q2:
        print(i.title,i.to_user)
    1 qqq 2 白金
    2 eee 1 普通
    3 ddd 2 白金
    4 rrr 4 超级
    普通 [<__main__.User object at 0x0000023886E5A630>]
    白金 [<__main__.User object at 0x0000023886E5A710>, <__main__.User object at 0x0000023886E5A5C0>]
    黑金 []
    超级 [<__main__.User object at 0x0000023886E5A0F0>]
    渐变 --> 突变
  • 相关阅读:
    java TopK算法
    夫妻过河问题
    Java中数据存储分配
    STM32与ARM代码执行过程
    C中gets()函数与scanf()函数说明
    MRTG开源监控安装手册
    mysql笔记
    windows调用ubuntu下的sublimeText2环境搭建
    linux性能优化
    测试那些事儿—软测必备的Linux知识(一)
  • 原文地址:https://www.cnblogs.com/lybpy/p/8280926.html
Copyright © 2020-2023  润新知