如何基于两列进行过滤,其中一列可能未使用 SQLAlchemy 定义?
How to filter based on two columns where one column may not be defined using SQLAlchemy?
我的运费 table 有 minimum_price 和 maximum_price。如果最小值为 0,最大值为 49,则如果值介于两者之间,则下面的查询有效。如果最小值是 50 美元并且未定义最大值,则它不起作用。我可以编写一个会得到正确结果的查询吗?
db.query(ShippingRate).filter(literal(myVal).between(ShippingRate.minimum, ShippingRate.maximum))
假设如果最小值或最大值不存在,则分别没有最大值或最小值,如果都不存在则所有值都为真,这样的事情怎么样:
from sqlalchemy import and_, or_
db.query(ShippingRate).filter(or_( \
and_(ShippingRate.minimum == None, ShippingRate.maximum == None), \
and_(ShippingRate.minimum != None, ShippingRate.maximum != None, literal(myVal).between(ShippingRate.minimum, ShippingRate.maximum)), \
and_(ShippingRate.minimum == None, myVal < ShippingRate.maximum), \
and_(ShippingRate.maximum == None, myVal > ShippingRate.minimum)))
我的运费 table 有 minimum_price 和 maximum_price。如果最小值为 0,最大值为 49,则如果值介于两者之间,则下面的查询有效。如果最小值是 50 美元并且未定义最大值,则它不起作用。我可以编写一个会得到正确结果的查询吗?
db.query(ShippingRate).filter(literal(myVal).between(ShippingRate.minimum, ShippingRate.maximum))
假设如果最小值或最大值不存在,则分别没有最大值或最小值,如果都不存在则所有值都为真,这样的事情怎么样:
from sqlalchemy import and_, or_
db.query(ShippingRate).filter(or_( \
and_(ShippingRate.minimum == None, ShippingRate.maximum == None), \
and_(ShippingRate.minimum != None, ShippingRate.maximum != None, literal(myVal).between(ShippingRate.minimum, ShippingRate.maximum)), \
and_(ShippingRate.minimum == None, myVal < ShippingRate.maximum), \
and_(ShippingRate.maximum == None, myVal > ShippingRate.minimum)))