SQLAlchemy 获取最大值

BC0*_*C00 2 python sql sqlalchemy flask-sqlalchemy

我试图在一个属性的表中获取最大值,该属性是风险:

我在跑步:

func.max(HostsModel.risk)
Run Code Online (Sandbox Code Playgroud)

我回来了:

<sqlalchemy.sql.functions.max at 0x10f0721d0; max>
Run Code Online (Sandbox Code Playgroud)

我如何真正从中获得价值?

我将这些视为可用功能:

 dir(func.max(HostsModel.risk))
['__add__', '__and__', '__bool__', '__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__or__', '__radd__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__rshift__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__visit_name__', '__weakref__', '_allow_label_resolve', '_alt_names', '_annotate', '_annotations', '_bind', '_bind_param', '_clone', '_cloned_set', '_cols_populated', '_compare_name_for_result', '_compiler', '_compiler_dispatch', '_constructor', '_copy_internals', '_deannotate', '_execute_on_connection', '_execution_options', '_from_objects', '_generate', '_hide_froms', '_init_collections', '_is_clone_of', '_is_from_container', '_is_join', '_is_lexical_equivalent', '_is_select', '_key_label', '_label', '_make_proxy', '_memoized_property', '_negate', '_order_by_label_element', '_params', '_populate_column_collection', '_refresh_for_new_column', '_render_label_in_columns_clause', '_reset_exported', '_resolve_label', '_select_iterable', '_textual', '_translate_schema', '_with_annotations', 'alias', 'all_', 'anon_label', 'any_', 'asc', 'base_columns', 'between', 'bind', 'bool_op', 'c', 'cast', 'clause_expr', 'clauses', 'coerce_arguments', 'collate', 'columns', 'comparator', 'compare', 'compile', 'concat', 'contains', 'correspond_on_equivalents', 'corresponding_column', 'count', 'desc', 'description', 'distinct', 'endswith', 'execute', 'execution_options', 'expression', 'filter', 'foreign_keys', 'get_children', 'identifier', 'ilike', 'in_', 'is_', 'is_clause_element', 'is_derived_from', 'is_distinct_from', 'is_selectable', 'isnot', 'isnot_distinct_from', 'join', 'key', 'label', 'lateral', 'like', 'match', 'name', 'named_with_column', 'notilike', 'notin_', 'notlike', 'nullsfirst', 'nullslast', 'op', 'operate', 'outerjoin', 'over', 'packagenames', 'params', 'primary_key', 'proxy_set', 'replace_selectable', 'reverse_operate', 'scalar', 'schema', 'select', 'selectable', 'self_group', 'shares_lineage', 'startswith', 'supports_execution', 'tablesample', 'timetuple', 'type', 'unique_params', 'within_group', 'within_group_type']
Run Code Online (Sandbox Code Playgroud)

bsp*_*ion 5

根据评论,您可以看到这与其他一些答案有关,但没有涵盖整个解决方案。重写,如果您想max(HostsModel.risk)直接访问该值:

session.query(func.max(HostsModel.risk)).scalar()
Run Code Online (Sandbox Code Playgroud)

您需要query使用该max函数,然后获取其scalar结果。由于此查询只能有一个结果(假设没有指定其他分组),您最终会得到一个等于最大值的HostsModel.risk值。

这也扩展到其他聚合,例如avg,min等。以这种方式利用标量也适用于过滤到单个列和记录时,即使没有定义聚合。

  • 这个建议对我有用。我大约 10 分钟前第一次看到它,认为它看起来没有吸引力,然后浪费时间寻找其他解决方案。对我来说,我得到: ```max_id = session.query(func.max(SqlPage.id)).scalar()``` 结果是类 'int' 的 6 就像我想要的那样。当然,您需要在文件顶部执行此操作:````from sqlalchemy import func``` (2认同)