【问题标题】:BIT_COUNT in SQLAlchemySQLAlchemy 中的 BIT_COUNT
【发布时间】:2012-10-15 22:32:31
【问题描述】:

我想在 SQLAlchemy 中表示以下查询:

select * from table where bit_count(column & bitmask) > 5

基本上,我希望选择设置了一定数量的标志的任何行。但是,SQLAlchemy 似乎没有定义 BIT_COUNT() 函数。有人知道在 SQLAlchemy 中进行此查询的任何技巧吗?

【问题讨论】:

    标签: python mysql sqlalchemy


    【解决方案1】:
    >>> session.query("id", "name", "thenumber12").\
    ...         from_statement("SELECT id, name, 12 as "
    ...                 "thenumber12 FROM users where bit_count(column&bitmask)<:the_val").\
    ...                 params(the_val=5).all()
    

    和我想象的差不多……

    >>> from sqlalchemy import create_engine
    >>> engine = create_engine('sqlite:///:memory:', echo=True)
    >>> engine.execute("select * from table where bit_count(column & bitmask) > 5").scalar()
    

    请记住,我在假设这是正确使用 bit_count 的情况下进行操作...

    【讨论】:

    • 谢谢,第一个表格是我最终使用的。它似乎最能利用 SQLAlchemy。
    猜你喜欢
    • 2018-02-27
    • 2014-01-28
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多