SQLAlchemy 不能在查询中使用 func.bigger 作为 func [英] SQLAlchemy can't use func.bigger as func in query
问题描述
描述我的问题.可以看到这个原始的sql:
To desc my problem. Can see this raw sql:
select datediff(now(), create_time) > 7 as is_new from test order by is_new desc limit 19;
我尝试通过 SQLAlchemy 一步一步实现:
I try to implement by SQLAlchemy step by step:
diff_days = func.datediff(today, test.create_time).label("diff_days")
session.query(diff_days).filter(test.id.in_((1,2,3,33344))).order_by(diff_days.asc()).all()
这个工作正常.但是当我想在 mysql 中描述 >
时.失败了:
This work fine. But when I want to desc >
in mysql. It failed:
is_new = func.greater(func.datediff(today, test.create_time), 7).label("is_new")
session.query(is_new).filter(test.id.in_((1,2,3,33344))).order_by(is_new.asc()).all()
我知道 SQLAlchemy 将我的 sql 解释为 greater
而 mysql 不支持.那么我怎样才能得到我的答案 a >b
类似 greater(a, b)
I know SQLAlchemy explain my sql to greater
while mysql don't support. So How can I to get my answer a > b
with something like greater(a, b)
可能是简单的sql select a >b from test
也可以说明问题.虽然以上是我的起源需要.所以问题可以改变:
May be the simple sql select a > b from test
can desc the problem too. While above is my origin need. So the problem can change :
如何使用SQLAIchemy orm来实现select a >b 来自测试
.
How to using SQLAIchemy orm to implement select a > b from test
.
推荐答案
SQLAlchemy 为您提供 丰富的运算符重载,所以就做
SQLAlchemy offers you rich operator overloading, so just do
is_new = (func.datediff(today, test.create_time) > 7).label("is_new")
session.query(is_new).\
filter(test.id.in_([1, 2, 3, 33344])).\
order_by(is_new.asc()).\
all()
自从创建Function
也是一个 ColumnElement
等具有 ColumnOperators
.
这篇关于SQLAlchemy 不能在查询中使用 func.bigger 作为 func的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!