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

本文详细介绍了使用SQL进行数据查询的各种技巧,包括条件过滤、排序、分组、并集操作等,并通过具体示例展示了如何运用filter、filter_by、order_by等方法来高效地管理和检索数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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',)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值