Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine query data store by a string start with

How to write a query that can find me all item_number start with a certain value ?

For instance there are item_numbers like these :

123_abc
123_xyz
ierireire
321_add
999_pop

My current query looks like this :

"select from "+PayPal_Message.class.getName()+" where item_number == '"+Item_Number+"' order by item_number desc"

What's a query look like that can return all item_numbers start with "123_" ?

like image 769
Frank Avatar asked May 24 '10 21:05

Frank


1 Answers

Quoted from the google app engine docs:

Tip: Query filters do not have an explicit way to match just part of a string value, but you can fake a prefix match using inequality filters:

db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2", "abc", u"abc" + u"\ufffd")

This matches every MyModel entity with a string property prop that begins with the characters abc. The unicode string u"\ufffd" represents the largest possible Unicode character. When the property values are sorted in an index, the values that fall in this range are all of the values that begin with the given prefix.

like image 186
baloo Avatar answered Oct 13 '22 17:10

baloo