在 Peewee 中执行子字符串查询 [英] Perform a substring query in Peewee
问题描述
我将 Python 2.7 与 Peewee 一起使用.此时,我需要使用 Peewee 来执行以下 SQL 查询:
I'm using Python 2.7 together with Peewee. At this moment, I need to use Peewee to execute the following SQL query:
select
a,
b,
substring(c, 1, 3) as alias1,
count(substring(c, 1, 3)) as alias2
from Table
where <some where condition, not relevant here>
group by a, alias1
我的第一个问题是如何使用 Peewee 执行子字符串.我已经搜索了文档,但到目前为止,运气不佳.
My first problem here is how to perform a substring with Peewee. I have searched the documentation and, so far, no lucky.
所以,基本问题是:我如何使用 Peewee 执行 substring
SQL 函数?如果有人能给我一些如何操作的指导,那也非常好使用 Peewee 执行上面的整个查询.
So, the basic question is: How do I perform a substring
SQL function using Peewee? It would also be very nice if someone can give me some directions of how to perform the entire query above with Peewee.
推荐答案
好的.
找了好久,终于找到了.它可以通过简单地使用 fn.substr
来完成.
After searching and searching, I finally found it. It can be done by simply using fn.substr
.
可以找到对该函数的引用这里.奇怪的是,fn
文档页面(那里只记录了 over
方法).
A reference to the function can be found here. Strangely, a documentation of this same function is not present in the fn
documentation page (only the method over
is documented there).
为了回答我自己的问题,SQL 查询将类似于(未测试):
To answer my own question, the SQL query is going to be something like (not tested):
TableModel.select(
a,
b,
fn.substr(c, 1, 3).alias('alias1'),
fn.count(fn.substr(c, 1, 3)).alias('alias2')
) \
.where(<some where condition, not relevant here>) \
.group_by(a, fn.substr(c, 1, 3))
希望这可以在未来帮助某人.
Hope this can help someone in the future.
这篇关于在 Peewee 中执行子字符串查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!