flask ORM查询集(query)的处理方法:

filter,filter_by,order_by,group_by,having,union_all,union,distinct,limit,offset

   # filter: 过滤; 语法:session.query(MyClass).filter(MyClass.name == 'some name')
    result0 = db.session.query(Protocols.protocolName).filter(Protocols.parent_protocol == "tcp,udp").all()
    # filter: 过滤; session.query(MyClass).filter(MyClass.name == 'some name', MyClass.id > 5)
    result1 = db.session.query(Protocols.protocolName).filter(Protocols.parent_protocol == "tcp,udp",
                                                              Protocols.id > 18).all()
    # filter_by:过滤; session.query(MyClass).filter_by(name = 'some name')
    result2 = db.session.query(Protocols.protocolName).filter_by(parent_protocol="tcp,udp").all()
    # filter_by:过滤; session.query(MyClass).filter_by(name = 'some name', id = 5)
    result3 = db.session.query(Protocols.protocolName).filter_by(parent_protocol="tcp,udp", id=3).all()
    # order_by:排序; asc(),desc()
    result4 = db.session.query(Protocols.protocolName).order_by(Protocols.id.asc()).all()
    result5 = db.session.query(Protocols.protocolName).filter_by(parent_protocol="tcp").order_by(
        Protocols.id.desc()).all()
    # group_by:分组;
    result6 = db.session.query(Protocols.parent_protocol).group_by(Protocols.parent_protocol)
    # having:分组后的过滤
    result7 = db.session.query(Protocols.parent_protocol).group_by(Protocols.parent_protocol).having(
        Protocols.parent_protocol.like("%cp%"))
    # union_all:并集(重复部分保存)
    result8 = result6.union_all(result7).all()
    result9 = result7.union_all(result6).all()
    # union:并集(重复部分剔除)
    result10 = result6.union(result7).all()
    # distinct:去重
    result11 = db.session.query(Protocols.parent_protocol).distinct().all()
    # limit:
    result12 = db.session.query(Protocols.protocolName).filter(Protocols.parent_protocol == "tcp").limit(6).all()
    # offset:跳过
    result13 = db.session.query(Protocols.protocolName).filter(Protocols.parent_protocol == "tcp").limit(6).offset(
        3).all()
0 [('browser',), ('atsvc',), ('rtsp',), ('dns',)]
1 [('rtsp',), ('dns',)]
2 [('browser',), ('atsvc',), ('rtsp',), ('dns',)]
3 []
4 [('ssh',), ('https',), ('telent',), ('rdp',), ('ntp',), ('http',), ('ftp',), ('vnc',), ('smb',), ('smtp',), ('pop3',), ('imap',), ('netstat',), ('netbios-ns',), ('netbios-dgm',), ('netbios-ssn',), ('browser',), ('atsvc',), ('rtsp',), ('dns',), ('bootpc',), ('snmp',), ('snmp trap',), ('bootps',), ('tftp',), ('dhcp',), ('icmp',), ('tcp',), ('udp',), ('esp',), ('ah',), ('sctp',)]
5 [('netbios-ssn',), ('netbios-dgm',), ('netbios-ns',), ('netstat',), ('imap',), ('pop3',), ('smtp',), ('smb',), ('vnc',), ('ftp',), ('http',), ('ntp',), ('rdp',), ('telent',), ('https',), ('ssh',)]
6 [('ip',), ('tcp',), ('tcp,udp',), ('udp',)]
7 [('tcp',), ('tcp,udp',)]
8 [('ip',), ('tcp',), ('tcp,udp',), ('udp',), ('tcp',), ('tcp,udp',)]
9 [('tcp',), ('tcp,udp',), ('ip',), ('tcp',), ('tcp,udp',), ('udp',)]
10 [('ip',), ('tcp',), ('tcp,udp',), ('udp',)]
11 [('tcp',), ('tcp,udp',), ('udp',), ('ip',)]
12 [('ssh',), ('https',), ('telent',), ('rdp',), ('ntp',), ('http',)]
13 [('rdp',), ('ntp',), ('http',), ('ftp',), ('vnc',), ('smb',)]

你可能感兴趣的:(Flask框架)