简体   繁体   English

如何编写对字段长度进行过滤的gql查询?

[英]How do I write a gql query that filters on the length of a field?

I want to filter a query by the length of one of the fields in a datastore but it appears I cannot include a len function in the WHERE clause. 我想按数据存储区中字段之一的长度来过滤查询,但看来我不能在WHERE子句中包含len函数。

q = db.GqlQuery("SELECT CommentCode FROM Comments " + 
                "WHERE RefObjType = 'paper' AND RefObjID = :1 AND len(CommentCode) = :2" +
                "ORDER BY CommentCode DESC ", RefObjID, 1)

This results in: BadQueryError: Parse Error: Invalid WHERE Condition at symbol ( 结果是: BadQueryError: Parse Error: Invalid WHERE Condition at symbol (

Can anyone give me an example of how I might get this to work? 谁能给我一个例子,说明如何使它起作用?

The RefObjID part works because the entire query works if I just leave out the len() filter. RefObjID部分有效,因为如果我只是省略len()过滤器,则整个查询都有效。

Thanks. 谢谢。

You can't (at least as far I understand it) because of how the DataStore works. 您不能(至少据我所知)是因为DataStore的工作原理。 The length of the string needs to be a property (either static or dynamic using db.Expando ) of your Model , and then you filter on that. 字符串的长度必须是Model的属性(使用db.Expando可以是静态的或动态的),然后对其进行过滤。

A possible solution: 可能的解决方案:
Modify the query to retrieve the results in DESC order and then process the results by accessing the required result after the query retrieval. 修改查询以按DESC顺序检索结果,然后在查询检索后通过访问所需结果来处理结果。

Change query: 变更查询:

  q = db.GqlQuery("SELECT CommentCode FROM Comments " + 
                    "WHERE RefObjType = 'paper' AND RefObjID = :1" +
                    "ORDER BY CommentCode DESC ", RefObjID, 1)

After this select the rows that have length(CommentCode)==2 with a loop and a conditional statement. 之后,选择具有循环和条件语句的length(CommentCode)==2的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM